node.js - 环回 REST findById 不能正常工作

标签 node.js rest loopbackjs strongloop

我想通过 REST API 使用 findById 函数。
我将“ID”定义为全部由数字构成的字符串。

我尝试通过ID查找,系统似乎可以识别它的编号。
当 ID 大于“9007199254740992”时,我无法使用它 - 最大整数数。
我想将 ID 用作字符串。

请告诉我如何解决这个问题。

谢谢,

--跟进--

我的程序如下。

模型 - sample-model.json

{
  "name": "SampleModel",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "id": {
      "type": "string",
      "id": "true",
      "required": true,
      "doc": "MODEL ID"
    },
    "prop1": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

当我通过 REST API 访问 findById 函数时,我总是收到以下调试消息。

  strong-remoting:shared-method - findById - invoke with +11ms [ 9007199254740992, undefined, [Function: callback] ]
  strong-remoting:shared-method - findById - result null +25ms
  strong-remoting:rest-adapter Invoking rest.after for SampleModel.findById +6ms
  express:router restRemoteMethodNotFound  : /api/SampleModels/9007199254740993 +143ms
  express:router restUrlNotFound  : /api/SampleModels/9007199254740993 +8ms
  express:router restErrorHandler  : /api/SampleModels/9007199254740993 +2ms
  strong-remoting:rest-adapter Error in GET /SampleModels/9007199254740993: Error: Unknown "SampleModel" id "9007199254740993".

最佳答案

我自己解决了我的问题。

我们可以使用“accepts”选项定义远程方法接受的参数。
内置的 findById 函数在 PersistedModel 中定义如下:

  accepts: [
    { arg: 'id', type: 'any', description: 'Model id', required: true,
      http: {source: 'path'}},
    { arg: 'filter', type: 'object',
      description: 'Filter defining fields and include'}
  ],

type 被定义为 any 时,id 通过 HttpContext.coerce 函数更改为数字 - 如果id 仅包含数字字符。

为了解决这个问题,我定义了SampleModel.findByIdCustom 并创建了另一个远程方法如下:

示例模型.js

SampleModel.findByIdCustom = function(id, filter, cb) {
  SampleModel.findById(id, filter, cb);
}

//Define remote method
SampleModel.remoteMethod(
  'findByIdCustom',
  {
    description: 'Find a model instance by id from the data source.',
    accessType: 'READ',
    accepts: [
        { arg: 'id', type: 'string', description: 'Model id', required: true,
          http: {source: 'path'}},
        { arg: 'filter', type: 'object',
          description: 'Filter defining fields and include'}
    ],
    returns: {arg: 'data', type: 'user', root: true},
    http: {verb: 'get', path: '/:id'},
    rest: {after: SampleModel.convertNullToNotFoundError},
    isStatic: true
  }
);

//disable built-in remote method
SampleMethod.disableRemoteMethod('findById', true);

谢谢,

关于node.js - 环回 REST findById 不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30255524/

相关文章:

javascript - 无法使用 res.send() 使用 express with node 发送数字

node.js - 将 Node 邮件程序 "from"更改为变量

javascript - Node js 事件循环中的微任务在哪里执行,它的阶段是如何排列优先级的?

javascript - 使用 node.js 上传文件时出错

java - "Content type not set"单元测试 Spring RESTful Controller 时

rest - grpc REST 网关中的 ETags 和 304 Not Modified 响应

asp.net - 寻找 RESTful API 的身份验证/模拟策略

mysql - LoopBack 4 REST API 使用 Mysql 获取记录的示例

javascript - React : statusCode: 400,名称: "Error",消息: "The limit parameter "未定义“无效

node.js - 使用 Loopback 滑动访问 token 的到期时间