user257980
user257980

Reputation: 1089

Loopback remote method relation List to JSON

I'm building custom remote methods to Loopback. When I'm doing query and trying to forward relations to own function where is forEach. Realtion list causes error relations.forEach is not a function

Example:

return Api.find({include: ['relations']}).then(result => {
    return myFunction(result.relations)
})

myFunction(relations) {relations.forEach(obj => {console.log(obj)})};

How I can convert List object to javascript array. toJSON() not working for List objects

Upvotes: 0

Views: 553

Answers (3)

F3L1X79
F3L1X79

Reputation: 2675

Have you tried to do the JSON conversion directly on your result? Like:

return Api.find({include: ['relations']}).then(result => {
    result = result.toJSON(); //add this line
    return myFunction(result.relations)
})

myFunction(relations) {relations.forEach(obj => {console.log(obj)})};

Upvotes: 1

akkonrad
akkonrad

Reputation: 1073

try to call result.relations() to get the array

Upvotes: 0

behzad besharati
behzad besharati

Reputation: 6910

if you need json string from an array of objects, then:

var jsonString=JSON.stringify({relations:relations});

Upvotes: 0

Related Questions