Pass array from backend to frontend


#1

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.


#2

You have to convert it to Json string.

{{{ toJson(data) }}}

#3

but my data is already converted to json:

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

attribute = attribute.toJSON();

i want to pass:
attribute.options


#4

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


#5

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

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

anyway many thanks