Pass array from backend to frontend


how to pass an array of objects from adonis to frontend javascript?

I know how to pass simple variables like:
var tab = ‘{{{ tab }}}’;

but this doesn’t work for arrays.


You have to convert it to Json string.

{{{ toJson(data) }}}


but my data is already converted to json:

let attribute = await Attribute
.where(‘id’, id).first();

attribute = attribute.toJSON();

i want to pass:


attribute.toJSON() returns a Javascript object and HTTP doesn’t know anything about Javascript objects. In short it has to be a JSON string when passing from backend to frontend.

The view toJSON helper is equivalent to calling JSON.stringify


your first answer was correct. but you had a typo, and that mislead me!

its {{{ toJSON(…) }}} , not {{{ toJson(…) }}}

anyway many thanks