将远程方法关系列表环回至JSON

时间:2019-01-30 06:23:44

标签: javascript node.js loopbackjs

我正在为Loopback构建自定义的远程方法。当我进行查询并尝试将关系转发到自己的功能时,forEach在哪里。房地产列表导致错误relations.forEach is not a function

示例:

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

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

如何将List对象转换为javascript数组。 toJSON()不适用于List对象

3 个答案:

答案 0 :(得分:1)

您是否尝试直接对结果进行JSON转换?喜欢:

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)})};

答案 1 :(得分:0)

如果您需要对象数组中的json字符串,则:

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

答案 2 :(得分:0)

尝试调用result.relations()获取数组