【问题标题】:Mongoose callback apply is not a functionMongoose 回调应用不是一个函数
【发布时间】:2018-07-27 17:38:31
【问题描述】:

我更新到猫鼬版本 4.10.5 ,但由于某种原因,现在我的聚合失败并出现以下错误:

   \node_modules\mongoose\lib\utils.js:214
            throw error;
            ^

TypeError: callback.apply is not a function
  at utils.promiseOrCallback.cb (C:\Users\Malin\Desktop\gameserver\node_modules\mongoose\lib\aggregate.js:693:14)
    at Object.promiseOrCallback (C:\Users\Malin\Desktop\gameserver\node_modules\mongoose\lib\utils.js:211:14)
    at Aggregate.exec (C:\Users\Malin\Desktop\gameserver\node_modules\mongoose\lib\aggregate.js:690:16)
    at Function.aggregate (C:\Users\Malin\Desktop\gameserver\node_modules\mongoose\lib\model.js:2809:13)

我需要编辑什么才能让这项工作再次发挥作用?

正在执行的代码:

return Account.aggregate(
        // Limit to relevant documents and potentially take advantage of an index
        { $match: {
            haveusername: true,
        }},

        { $project: {
            total: { $add: ["$cash", "$bank"] }
        }}
    ).sort({total: -1}).limit(10).then(function (richest) {
//something else here

});

【问题讨论】:

  • 如何将sortlimit 包含在聚合管道中?
  • 是的,这可能是一些东西,但我不太擅长聚合 atm。
  • 让我发布一个我将如何做的答案(但我不确定then 链接)。

标签: javascript mongodb mongoose aggregate


【解决方案1】:

尝试将 sortlimit 链接方法更改为聚合调用中的阶段:

return Account.aggregate(
        // Limit to relevant documents and potentially take advantage of an index
        { $match: {
            haveusername: true,
        }},
        { $project: {
            total: { $add: ["$cash", "$bank"] }
        }},
        { $sort: {total: -1} },
        { $limit : 5 }
    ).then(function (richest) {
        //something else here
    });

【讨论】:

  • MongooseError: Mongoose 5.x 不允许将一系列运算符传递给Model.aggregate()。而不是Model.aggregate({ $match }, { $skip }),做Model.aggregate([{ $match }, { $skip }])
猜你喜欢
  • 2017-08-28
  • 2016-03-18
  • 1970-01-01
  • 2017-10-13
  • 2016-08-09
  • 1970-01-01
  • 2018-10-11
  • 2016-09-25
  • 2020-09-16
相关资源
最近更新 更多