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

标签 node.js express loopback

更新属性时我有一个 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/

相关文章:

javascript - 如何在 Loopback 3 中定义管理员角色?

json - Loopback:使用 REST 连接器发送带有 json 数据的请求

node.js - 为什么我在查询用户项时会出现 401 错误?

node.js - 在一个 docker 镜像中运行两个 nodejs 应用程序

node.js - 在 NestJS 中创建代理

javascript - 我的正则表达式在 Node.js 中不起作用?

javascript - 使用 node.js express 从数据库获取和处理数据

node.js - React应用程序中的操作响应状态代码

javascript - setInterval函数无法读取数组

node.js - 通过 Node js Express 访问 AWS S3 签名 URL