【问题标题】:Loopback post method call环回 post 方法调用
【发布时间】:2018-09-04 15:32:43
【问题描述】:

我想发送一个带有环回“invokeStaticMethod”的post请求。 请帮我怎么做。 我想向以下网址发送 POST API 请求:

localhost:3000/api/user/id/unblock 带参数{"userId", "blockId"}

请告诉我如何使用 Loopback 发送 POST 请求

【问题讨论】:

标签: android loopbackjs strongloop loopback


【解决方案1】:

你可以像这样创建一个远程方法:

User.unblock = function(id, userId, blockId, callback) {
  var result;
  // TODO
  callback(null, result);
};

那么,json 文件中的远程方法定义可能如下所示:

"unblock": {
  "accepts": [
    {
      "arg": "id",
      "type": "string",
      "required": true,
      "description": "",
      "http": {
        "source": "path"
      }
    },
    {
      "arg": "userId",
      "type": "string",
      "required": false,
      "description": "",
      "http": {
        "source": "form"
      }
    },
    {
      "arg": "blockId",
      "type": "string",
      "required": false,
      "description": "",
      "http": {
        "source": "form"
      }
    }
  ],
  "returns": [
    {
      "arg": "result",
      "type": "object",
      "root": false,
      "description": ""
    }
  ],
  "description": "",
  "http": [
    {
      "path": "/:id/unblock",
      "verb": "post"
    }
  ]
}

那么您的远程方法将如下所示:

您可以使用函数参数并使用一个主体参数而不是 2 个表单参数并从那里读取数据,尽管我相信如果只有 2 个附加参数,最好将它们分开放置。但这取决于你的方法。

【讨论】:

    【解决方案2】:

    我相信这就是你要找的……

    https://loopback.io/doc/en/lb3/Adding-remote-methods-to-built-in-models.html

    在你的情况下,它应该看起来像这样......

    module.exports = function(app) {
      const User = app.models.User;
    
      User.unblock = function(userId, blockId, cb) {
          ... <Your logic goes here> ...
          cb(null, result);
      };
    
      User.remoteMethod('unblock', {
          accepts: [{arg: 'userId', type: 'string'}, {arg: 'blockId', type: 'string'}],
          returns: {arg: 'result', type: 'string'}
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多