0
votes

I know the difference between a callback and a middleware next() function.

If a write a custom remote-method in Loopback, I can easily send errors in callback e.g callback(error,null) but in remote hooks or observers whenever I send error in next() function e.g

var err = new Error('This is error'); next(err)

it always says that Internal server error but it does not tell me what error is. In order to view error I have to login to server and view logs. Please tell me how can I send error as a response in next() function so that the on frontend I know what error has occurred.

2

2 Answers

0
votes

Maybe use a middleware to hook in:

app.use( function(err,req,res){
  res.json(err);
});

(This needs to he the last middleware defined...)

0
votes

basically you can define callbacks with err and result. For example in loopback, if I have a model call "Action" you can simply send err or result to front end using json.

 app.get('/your/api/call', function (req, res, next) {
    var getTeam = function (cb) {
      app.models.Team.find({}, function (err, teams) {
        if (err) { 
          cb(err);
        } else { 
          cb(null, teams);
        }
      });
    };
    async.waterfall([
      getTeam
    ], function (err, team, role) {
      if (err){
res.send(err); //send error  to front end
} else {
 res.send(team); //send result  to front end
}

    });
  });

This approach can use with "app.use" function in root level also.