【发布时间】:2019-02-28 22:01:24
【问题描述】:
我能够从 findById 函数内部推送结果,并且日志打印正确,但在 forEach 循环之外它只是空的。我确实尝试过使用 Promise,但这也没有用。
let finalData = [];
Model.afterRemote('find', function(ctx, instance, next) {
if (ctx.result) {
if (Array.isArray(ctx.result)) {
ctx.result.forEach(function(result) {
Model.findById(result.id, {
include: {
relation: 'users',
scope: {
fields: ['email']
}
}
},
function(err, response) {
finalData.push(response);
console.log(finalData); // has the user information
});
});
}
ctx.result = finalData;
console.log(ctx.result); // --> empty result (need the finalData result)
}
next();
});
【问题讨论】:
-
我发现第一个get请求是空的,但是后面的get请求返回正确的数据。
标签: javascript loopbackjs loopback