Search code examples
loopbackjsstrongloop

loopback Add non static remote method error


I am trying to add a non-static remote method to a model. Just follow the code here. Unfortunately, I got some error message.

The following is my code

User.prototype.lastOrder = function(callback){
  console.log('print this instance object: ', this);
  callback(null)
};

User.remoteMethod('__get__lastOrder', {
  isStatic: false,
  accepts: [],
  description: 'Get the latest order of the user',
  http: {
    path: '/lastOrder',
    verb: 'get'
}

And when I invoke http://localhost:3000/v1/users/1/lastOrder. it gives me the following error:

enter image description here


Solution

  • The first argument to remoteMethod is the function name. What you have defined isn't valid. You need to define a function called, well, let's say lastOrder, and then modify your code like so:

    User.prototype.lastOrder = function() {
    
    }
    
    User.remoteMethod('lastOrder', {
      isStatic:false,
      //more stuff here
    }