【问题标题】:How to include a relation to the result of a remote method in loopback?如何在环回中包含与远程方法结果的关系?
【发布时间】:2018-11-24 00:28:50
【问题描述】:

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

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

    next();
});

我做错了什么?返回的结果中不包含“images”属性。

【问题讨论】:

    标签: node.js express loopback


    【解决方案1】:

    我猜它不起作用,因为“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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2017-06-30
      相关资源
      最近更新 更多