Marcos Maia Marcos Maia - 1 year ago 117
Node.js Question

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) {
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": ""

Any ideas what could be the problem? I've been stuck with this for a while and I guess the solution is simple..

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.

Answer Source

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

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