Joe SHI
Joe SHI

Reputation: 1796

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

Upvotes: 2

Views: 880

Answers (2)

Raymond Camden
Raymond Camden

Reputation: 10857

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
}

Upvotes: 3

Joe SHI
Joe SHI

Reputation: 1796

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

  User.remoteMethod('lastOrder', {  // should be lastOrder not __get__lastOrder
    isStatic: false,
    accepts: [],
    description: 'Get the latest order of the user',
    http: {
      path: '/lastOrder',
      verb: 'get',
      status: 200
    },
    returns: {root: true, type: 'order'}
  });

Upvotes: 3

Related Questions