0

I am new to loopback and I am trying to create a simple remote method in loopback that uses findyById method. Been spending couple of hours on this and still can't get it to work. Here is my code:

customer.js:

    Customer.list = function(customerId, cb){
       app.models.Customer.findById(customerId, function (err, instance) {
          cb(null, err || 'success');
          if(err){
             console.log(err);
          }else if(instance){
             console.log(instance);
          }
       });
    }

    // expose the above method through the REST
   Customer.remoteMethod('list', {
       returns: {
          arg: 'list',
          type: 'string'
       },
       accepts: {arg: 'id', type: 'number', http: { source: 'query' } },
       http: {
          path: '/list',
          verb: 'get'
       }
   });

customer.controller.js:

    Customer.list(1)
            .$promise
            .then(function(response){
                console.log(response);
            })
            .catch(function(err){
                console.log(err);
            });

My customer row in mysql:

id: 1 number: 10

I get this error:

    Error: Model::findById requires the id argument
    at Desktop\SampleProject\node_modules\loopback-datasource-juggler\lib\dao.js:1287:10
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Can you please tell me the possible reason/s why I get this error? Please help me. Thank you

Ebrahim Pasbani
  • 9,168
  • 2
  • 23
  • 30
Alisa
  • 49
  • 1
  • 10

1 Answers1

1

With get verb, there is no body.

You need to change the remote method like this :

 accepts: {arg: 'id', type: 'number', http: { source: 'path' } },
       http: {
          path: '/list/:id',
          verb: 'get'
       }
Ebrahim Pasbani
  • 9,168
  • 2
  • 23
  • 30