【问题标题】:Loopback order filter when using find in a remote method from a model, error:在模型的远程方法中使用 find 时的环回顺序过滤器,错误:
【发布时间】:2016-08-20 15:54:35
【问题描述】:

我很难在远程方法的查找中使用简单的排序过滤器:

    /**
 * 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'}}
  ]
});

在您看到我的远程/查找实现的上方,它可以在没有订单过滤器的情况下正常工作。一旦我添加了 oder {mealDate: 'DESC'} 我得到一个错误:

订单 {"mealDate":"DESC"} 无效

mealDate 是我模型上的 Date 类型。

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

任何想法可能是什么问题?我已经坚持了一段时间,我想解决方案很简单..

P.S - 我知道我可以在数组中直接使用排序来执行此操作,但在这种情况下我尝试使用环回过滤器。

【问题讨论】:

    标签: node.js sorting loopbackjs loopback


    【解决方案1】:

    基于doc,我觉得应该是这样的:

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

    【讨论】:

    • 是的,我不知道我一开始怎么会错过。
    【解决方案2】:

    环回 4:

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

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 2017-04-06
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      • 2017-03-29
      • 2014-09-02
      • 2023-04-03
      • 2011-10-01
      相关资源
      最近更新 更多