node.jssortingloopbackjsloopback

Loopback order filter when using find in a remote method from a model, error:


I am having a hard time with a simple ordering filter in a find in a remote method:

    /**
 * This remote method exposes the meals history from the current logged in user
 */
Meal.listMeals = function(req, res, cb) {
  Meal.find({
    where: {patientId: req.accessToken.userId},
    order: {mealDate: 'DESC'}
  }, cb);
};
Meal.remoteMethod('listMeals', {
  returns: {arg: 'meals', type: 'array'},
  http: {path:'/list-meals', verb: 'get'},
  accepts: [
    {arg: 'req', type: 'object', http: {source: 'req'}},
    {arg: 'res', type: 'object', http: {source: 'res'}}
  ]
});

Above you see my remote / find implementation, it works properly without the order filter.. once I add that oder {mealDate: 'DESC'} I get an error:

The order {"mealDate":"DESC"} is not valid

mealDate is a Date type on my model.

"properties": {
"mealDate": {
  "type": "date",
  "required": true,
  "default": "Date.now"
},

What could be the problem?

P.S - I know I could use sort direct in the array to do this but I am trying to use loopback filters in this case.


Solution

  • Based on the doc, I think it should be like this:

    Meal.find({
      where: {patientId: req.accessToken.userId},
      order: 'mealDate DESC' 
    }, cb);