Loopback: Remote methods with conditional responses

Created on 16 Aug 2016  路  1Comment  路  Source: strongloop/loopback

I hope this is the right place to ask something like this.
Im currently implementing a few remothe methods for some of my models. A lot of them use for example Model.findById(...), and then return data based on the found result.

Dog.location = function(id, cb) {

  Dog.findOne({where: {id: id}}, function(err, dog) { 

    cb(null, dog.location);
  });    
}

This was a good example from a blogpost (https://strongloop.com/strongblog/remote-methods-in-loopback-creating-custom-endpoints/) which in its basics is the same thing i would like to do. This example assumes though that a dog object is found and if not it will probably throw an error since it wont be able to read the attribut '.location' from undefined.
What I would like to do is to send a 404 response when such a case happens. But my problem is that you have to predefine in your .remoteMethod() how your response will always look like.
How could I define it conditionally (if dog is found return location, else return 404)?

Most helpful comment

Hi @Schubbcasten : Welcome to LoopBack community :)

Re. remoteMethods: you can approach error propagation as follows:

  • If you want to set a general statusCode for any failures with your remote method, it can be achieved while defining the remote method as follows:
MyModel.remoteMethod(
    'greet',
    {
      accepts: { arg: 'msg', type: 'string' },
      returns: [{ arg: 'greeting', type: 'string' }],
      http:{ errorStatus: '400' }
    }
  );

In above, any error that may occur in your greet method would have 400 status code by default.

Now, to customize what you want to send (for example in your case, 404), you can define your remote method as:

Dog.location = function(id, cb) {
  Dog.findOne({where: {id: id}}, function(err, dog) {
    if (err) {
      err.statusCode = '404'; // assuming that err is an Error object
      cb(err);
    }
    cb(null, dog.location);
  });
}

In above, let's say, you provide your own method implementation, then also you can create your own error object, set the status and call cb() with it.

Note: The status code here will override the default errorStatus set at time of defining remote method.

Hope that helps. Closing this for now. if you need further assistance, please mention my GH handle in the comments and I'll be happy to assist you.

Wish you the best in your LoopBack journey!

>All comments

Hi @Schubbcasten : Welcome to LoopBack community :)

Re. remoteMethods: you can approach error propagation as follows:

  • If you want to set a general statusCode for any failures with your remote method, it can be achieved while defining the remote method as follows:
MyModel.remoteMethod(
    'greet',
    {
      accepts: { arg: 'msg', type: 'string' },
      returns: [{ arg: 'greeting', type: 'string' }],
      http:{ errorStatus: '400' }
    }
  );

In above, any error that may occur in your greet method would have 400 status code by default.

Now, to customize what you want to send (for example in your case, 404), you can define your remote method as:

Dog.location = function(id, cb) {
  Dog.findOne({where: {id: id}}, function(err, dog) {
    if (err) {
      err.statusCode = '404'; // assuming that err is an Error object
      cb(err);
    }
    cb(null, dog.location);
  });
}

In above, let's say, you provide your own method implementation, then also you can create your own error object, set the status and call cb() with it.

Note: The status code here will override the default errorStatus set at time of defining remote method.

Hope that helps. Closing this for now. if you need further assistance, please mention my GH handle in the comments and I'll be happy to assist you.

Wish you the best in your LoopBack journey!

Was this page helpful?
0 / 5 - 0 ratings