【问题标题】:How to set custom schema for custom remote methods on Strongloop如何在 Strongloop 上为自定义远程方法设置自定义架构
【发布时间】:2016-06-17 23:40:31
【问题描述】:

我是 Strongloop 的新手,我找不到有关如何自定义我的响应类(我构建的对象的模型架构)的信息,我不知道如何在 API 资源管理器上显示带有自定义数据的对象.

例如,我有一个名为 score 的自定义远程方法

POST /Challenges/score

我想为参数data 显示自定义模型架构而不是单个参数,而不是挑战的模型架构,主体上的数据具有所有参数并在数据类型上显示给用户:模型架构,这可能吗?

{
  "id": "string",
  "limit": 0,
  "order": "string",
  "userId": "string"
}

另一方面,在响应类中,我想显示响应对象的架构。像这样的:

{
  "id":"string",
  "userId":"string",
  "user": {},
  "totalScore":0,
  "tags": []
}

我查看了不同的问题(thisthis),但找不到解决此问题的方法。

更新

这里是远程方法的定义

Challenge.remoteMethod('score', {
    accepts: { arg: 'data', type: 'object', http: { source: 'body' } },
    returns: {arg: 'scores', type: 'array'},
    http: {path: '/score', verb: 'post'}
});

【问题讨论】:

  • 请告诉我们您是如何定义远程方法的。
  • @RaymondCamden 我已经用远程方法更新了问题
  • 好的,所以我无法解析您的确切问题。您是说您返回一组自定义数据并希望将其记录在案吗?如果是这样,你能显示你用来生成结果的代码(分数)吗?您是否还说要为输入定义“数据”?
  • 实际上,对于“接受”部分,您真的想要一个名为数据的对象,还是想要一组键/值对?我认为您只需要密钥/值对,因为您没有定义任何其他人
  • 好吧,也许我没有正确解释自己。对于“接受”部分,我想要一个名为 data 的对象,其中包含自定义键/值对。类似于单个参数的东西,但不是参数和对象体内的东西。对于响应,我想为我构建的对象展示一种方法(使用包含或订单等内部过滤器)。总之,我将 explorer api 提供给外部用户,然后他们使用自定义服务,他们知道要发送数据对象中的哪些键/值以及 api 响应对象是什么。

标签: methods model response loopbackjs strongloop


【解决方案1】:

我找到了解决这个问题的方法,方法是更改​​接受数组中的类型参数。 当我们创建远程方法时;我们提供接受数组。有arg、type、required、http。然后我们可以将我们的请求对象放入 type 参数中。

示例代码

UserModel.remoteMethod(
 'login',
  {
    description: 'Login a user with username/email and password.',
    accepts: [
      {
        arg: 'credentials',
        type: {'email': 'string', 'password': 'string'},
        required: true,
        http: {source: 'body'},
      },
      {
        arg: 'include', type: ['string'], http: {source: 'query'},
        description: 'Related objects to include in the response. ' +
          'See the description of return value for more details.',
      },
    ],
    returns: {
      arg: 'accessToken', type: 'object', root: true,
      description:
        g.f('The response body contains properties of the {{AccessToken}} created on login.\n' +
          'Depending on the value of `include` parameter, the body may contain ' +
          'additional properties:\n\n' +
          '  - `user` - `U+007BUserU+007D` - Data of the currently logged in user. ' +
          '{{(`include=user`)}}\n\n'),
    },
    http: {verb: 'post'},
  },
);

【讨论】:

    【解决方案2】:

    在环回中,远程参数可以识别已使用 ds.define('YourCustomModelName', dataFormat); 定义的数据模型

    因此,对于您的情况,在 Challenge.js 文件中编写一个函数,该文件将定义一个远程方法(在您的情况下为 score)。

    const loopback = require('loopback');
    const ds = loopback.createDataSource('memory'); 
    module.exports = function(Challenge) {
      defineChallengeArgFormat() ;
     // remote methods (score) defined
    };
    
    let defineChallengeArgFormat = function() {
      let dataFormat = {
                "id": String,
                "userId": String,
                "user": {},
                "totalScore": Number,
                "tags": []
            };
      ds.define('YourCustomModelName', dataFormat);
    };
    

    在远程参数类型下使用 'type': 'YourCustomModelName'

        Challenge.remoteMethod('score', {
            accepts: {
                arg: 'data',
                type: 'YourCustomModelName',
                http: {
                    source: 'body'
                }
            },
            returns: {
                arg: 'scores',
                type: 'Challenge'
            },
            http: {
                path: '/score',
                verb: 'post'
            }
        });
    

    重启服务器并刷新后,您应该会看到它反映在资源管理器中:)

    【讨论】:

      【解决方案3】:

      @jrltt,不要使用 default,而是使用指向 accepts 下的 type 的对象结构,它应该可以工作。注意,http source:body 是必需的。

      随机物体:

      Challenge.remoteMethod('score', {
          accepts: {
              arg: 'data',
              type: {
                  "id": "string",
                  "userId": "string",
                  "user": {},
                  "totalScore": 0,
                  "tags": []
                },
              http: {
                  source: 'body'
              }
          },
          returns: {
              arg: 'scores',
              type: 'Challenge'
          },
          http: {
              path: '/score',
              verb: 'post'
          }
      });
      

      使用模型配置中可用的定义模型或使用环回模型生成器创建,然后该模型名称可用于指向类型。 所以让我们使用 User 模型在接受参数中显示,

      Challenge.remoteMethod('score', {
          accepts: {
              arg: 'data',
              type: 'User',
              http: {
                  source: 'body'
              }
          },
          returns: {
              arg: 'scores',
              type: 'Challenge'
          },
          http: {
              path: '/score',
              verb: 'post'
          }
      });
      

      【讨论】:

        【解决方案4】:

        我发现解决这个问题的方法是这样创建一个新模型,用helper slc loopback: model

        ? Enter the model name: ArgChallenge
        ? Select the data-source to attach ArgChallenge to: (no data-source)
        ? Select model's base class PersistedModel
        ? Expose ArgChallenge via the REST API? No
        ? Common model or server only? server
        

        我继续放置属性,然后在 Challenge.js 上:

        Challenge.remoteMethod('score', {
            accepts: { arg: 'data', type: 'ArgChallenge', http: { source: 'body' } },
            returns: {arg: 'scores', type: 'array'},
            http: {path: '/score', verb: 'post'}
        });
        

        这行得通!如果有人知道更好的方法,请分享。

        【讨论】:

          【解决方案5】:

          我相信您可能已经阅读过 strongloop 的官方文档。如果没有,这里是解释远程方法及其接受的数据类型的链接。 https://docs.strongloop.com/display/public/LB/Remote+methods

          假设您的自定义对象是 Challenge,要显示对象作为响应,您必须指定类型(类型可以是环回的数据类型之一或您的自定义模型)。所以要返回 Challenge 你必须添加以下代码:

          Challenge.remoteMethod('score', {
              accepts: { arg: 'data', type: 'object', http: { source: 'body' } },
              returns: {arg: 'scores', type: 'Challenge', root: true},
              http: {path: '/score', verb: 'post'}, 
          });
          

          您指定的第二个箭头是您希望通过 API 调用尝试的默认值。您可以使用 default 作为键传递任何自定义字符串。 例如,如果你想传递一些对象:

          Challenge.remoteMethod('score', {
              accepts: {
                  arg: 'data',
                  type: 'object',
                  default: '{
                      "id": "string",
                      "userId": "string",
                      "user": {},
                      "totalScore": 0,
                      "tags": []
                  }',
                  http: {
                      source: 'body'
                  }
              },
              returns: {
                  arg: 'scores',
                  type: 'Challenge'
              },
              http: {
                  path: '/score',
                  verb: 'post'
              }
          });
          

          因此,对于响应,您不能自定义模型。但是要传递默认值,您可以将任何内容放在字符串格式中。

          【讨论】:

          • 对不起!起初我认为这是一个简单的想法,但没有时间尝试。昨天我去尝试了,直到今天我都无法回答你,当定义一个默认值时,loopback 忽略它并继续显示基本模型的结构
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-29
          • 1970-01-01
          • 1970-01-01
          • 2019-12-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多