【问题标题】:Include Loopback relation in POST response在 POST 响应中包含 Loopback 关系
【发布时间】:2019-02-12 23:14:36
【问题描述】:

我有一个正在工作的聊天室环回项目,它也使用了 Socket.IO。

创建消息(POST 到 Loopback REST API)后,我需要包含“创建者”关系的响应。

这在使用 GET 时可以正常工作,但我不能包含与 POST 响应的关系。

我确定这是一个简单的远程挂钩,但我被卡住了......

非常感谢任何帮助!

"relations": {
  "creator": {
    "type": "belongsTo",
    "model": "Person",
    "foreignKey": "",
    "options": {
      "nestRemoting": true
    }
  },
  "event": {
    "type": "belongsTo",
    "model": "Event",
    "foreignKey": "",
    "options": {
      "nestRemoting": true
    }
  }
},

【问题讨论】:

  • 您是否使用环回开箱即用的默认 POST 方法?您可以尝试覆盖它以添加一些自定义行为。你试过了吗?
  • 嗨,彼得。我在做。我正在寻找一种方法来做到这一点,而不必创建自定义方法......
  • 顺便说一句,您使用哪个版本的环回?是 2.x 还是 3.x?
  • 嗨@James Clare,下面的答案对你有用吗?

标签: loopback


【解决方案1】:

您可以通过两种方式使用 Loopback 2.x3.x(不确定 Loopback 4.x)。

假设我们有以下"Note" 模型:

{
  "name": "Note",
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "content": {
      "type": "string"
    },
    "userId": {
      "type": "number"
    }
  },
  "relations": {
      "user": {
          "type": "belongsTo",
          "model": "User",
          "foreignKey": "userId"
      }
  }
}

现在,当您 create (POST) 时,在响应中包括 "user" 属性(这是 NotebelongsTo 关系)请注意,您有两个选择。

选项 #1推荐):创建 custom remote method 并在模型的脚本文件中隐藏默认方法。在这种情况下,您的 note.js 文件应该类似于:

module.exports = function (Note) {
    // Hide the default 'create' remote method
    Note.disableRemoteMethod('create', true);

    // Add a custom 'customCreate' remote method 
    Note.remoteMethod('customCreate', {
        description: 'Create a new instance of the model and persist it into the data source.',
        accessType: 'WRITE',
        accepts: [
            {
                arg: 'data',
                type: 'object',
                model: 'Note',
                allowArray: true,
                description: 'Model instance data',
                http: { source: 'body' },
            },
            { arg: 'options', type: 'object', http: 'optionsFromRequest' },
        ],
        returns: { arg: 'data', type: 'Note', root: true },
        http: { verb: 'post', path: '/' },
        isStatic: true,
    });

    Note.customCreate = function (data, options, cb) {
        Note.create(data, options, function(err, newObj) {
            if (err) {
                cb(err);
            }
            else {
                // here we try to load the user value
                newObj.user(function (err, user) { 
                    if (user) {
                        // if we found a user we add it to __data, so it appears in the output (a bit hacky way)
                        newObj.__data.user = user;
                    }

                    cb(err, newObj);
                });
            }
        });
    };
};

我建议使用此选项,因为您只需对环回模型的默认逻辑进行最少的更改即可实现所需的功能,即所有默认方法(如 create、upsert 等)继续具有默认行为。

选项 2:使用“保存后”operation hook(请谨慎使用此方法,因为它会改变 create、upsert、upsertWithWhere 和其他默认方法的工作方式)

在这种情况下,您的 note.js 文件应如下所示:

module.exports = function (Note) {
    Note.observe('after save', function (ctx, next) {
        ctx.instance.user(function (err, user) {
            ctx.instance.__data.user = user;
            next();
        });
    });
};

第二个选项的代码更少,但正如我之前提到的,你应该非常小心地使用它,因为它会改变模型默认“create”方法的行为。 IE。每次调用 Model.create、Model.upsert 等时都会执行 'after save' 操作。当您在 'after save' 挂钩中添加额外的选择查询时,它也会减慢这些操作。

【讨论】:

    猜你喜欢
    • 2019-03-26
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 2020-01-30
    • 2018-07-01
    • 1970-01-01
    • 2015-08-21
    相关资源
    最近更新 更多