更新属性时我有一个 beforeRemote 方法。我在 context.args.include 中包含了一个关系,但返回的结果不包含该关系。这是示例代码
Assessment.beforeRemote('prototype.updateAttributes', function(context, data, next){
context.args.include = ['images'];
next();
});
我做错了什么? “images”属性未包含在返回的结果中。
最佳答案
我想它不起作用,因为“updateAttributes”不使用环回过滤器来检索更新的值,它不是通常的“查找”请求。
我会使用 afterRemote 而不是 beforeRemote 并添加代码来检索数据:
Assessment.afterRemote('prototype.updateAttributes', function(ctx, modelInstance, next) {
if (!ctx.result) return next();
if (Array.isArray(modelInstance)) {
Assessment.find({
where: {id: {inq: modelInstance.map(instance => instance.id)}},
include: ['images']
}).then(data => {
// ctx.result is sent to client
ctx.result = [].concat(data);
next();
}).catch(err => {
// pass error to next
next(err);
});
}else{
Assessment.findOne({
where: {id: modelInstance.id)},
include: ['images']
}).then(data => {
// ctx.result is sent to client
ctx.result = Object.assign({}, data);
next();
}).catch(err => {
// pass error to next
next(err);
});
}
});
详细信息在这里:loopback 3 remote hooks
关于node.js - 如何在环回中包含与远程方法结果的关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53424513/