如何在环回中包含与远程方法的结果的关系?

时间:2018-11-22 05:39:44

标签: node.js express loopback

更新属性时,我有一个beforeRemote方法。我在context.args.include中包含了一个关系,但是返回的结果不包含该关系。这是示例代码

Assessment.beforeRemote('prototype.updateAttributes', function(context, data, next){
    context.args.include = ['images'];

    next();
});

我在做什么错?返回的结果未包含“图像”属性。

1 个答案:

答案 0 :(得分:0)

我猜这是行不通的,因为“ 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