简体   繁体   中英

Loopback REST findById doesn't work well

I'd like to use findById function through REST API.
I defined "ID" as string all constructed by number.

I try to find by ID, the system seems to recognize it number.
I can't use it when the ID is a big number over "9007199254740992" - max number of integer.
I'd like to use ID just as string.

Please tell me how to solve this problem.

Thank you,

--follow up--

My program is as follow.

Model - 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": []
}

When I access to findById function through REST API, I always get following debug message.

  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".

I resolved my question by myself.

We can define arguments that the remote method accepts using "accepts" option.
Built-in findById function defines as follow at PersistedModel:

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

When the type is defined any , the id changes to number by HttpContext.coerce function - if the id consists only number chars.

To solve this problem, I defines SampleModel.findByIdCustom and create another remote method as follow:

SampleModel.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);

Thank you,

Just set idInjection to false (so that loopback doesnt automatically add an id property to your model), then define a property with the following parameters:

{
  "idInjection": false,
  "properties": {
    "id": {
      "type": "string",
      "id": true,
      "generated": true
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM