环回远程方法关系列表到 JSON

Loopback remote method relation List to JSON

我正在为 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() 不适用于列表对象

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

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

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

您是否尝试过直接对结果进行 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)})};