node.js - 错误:参数必须是 Aggregate.append 处的聚合管道运算符
问题描述
这是我的应用程序提供的堆栈跟踪:
Error: Arguments must be aggregate pipeline operators
at Aggregate.append (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/mongoose/lib/aggregate.js:89:11)
at new Aggregate (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/mongoose/lib/aggregate.js:48:17)
at Function.aggregate (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/mongoose/lib/model.js:2415:17)
at Function.storeSchema.statics.getTopStores (/Users/flywheel/zprojects/whatsgood/the-ashevillian/models/Store.js:82:15)
at exports.getTopStores (/Users/flywheel/zprojects/whatsgood/the-ashevillian/controllers/storeController.js:198:30)
at /Users/flywheel/zprojects/whatsgood/the-ashevillian/handlers/errorHandlers.js:11:12
at Layer.handle [as handle_request] (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/layer.js:95:5)
at /Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:335:12)
at next (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:174:3)
at router (/Users/flywheel/zprojects/whatsgood/the-ashevillian/node_modules/express/lib/router/index.js:47:12)
相关代码——这些是整个应用程序中唯一调用聚合的两个地方:
Store.js
storeSchema.statics.getTagsList = function() {
return this.aggregate([
{ $unwind: '$tags' },
{ $group: { _id: '$tags', count : { $sum: 1 } }},
{ $sort: { count: -1} }
]);
};
storeSchema.statics.getTopStores = function (){
return this.aggregate([
// Look up stores and populate reviews
{ $lookup: { from: 'reviews', localField: '_id',
foreignField: 'store', as: 'reviews'}},
// Filter for stores with 2 or more reviews
{ $match: { 'reviews.1': { $exists: true } }},
// Add average reviews field
// TODO: Changed with $addfield in mongodb 3.4?
// UPDATE
{ $project: {
photo: '$$ROOT.photo',
name: '$$ROOT.name',
reviews: '$$ROOT.reviews',
slug: '$$ROOT.slug',
averageRating: { $avg: '$reviews.rating' }
} },
// sort it by our new field, highest reviews first
{ $sort: { averageRating: -1 }},
// limit to at most 10
{ $limit: 10 }
])
};
到目前为止,我试图找到解决这个问题的方法,导致我找到了一些不同或不清楚的示例,或者没有解决错误消息的文档。当然,发生了什么是应该在代码块中的这两个函数中使用一些聚合管道运算符,但我不知道是什么。
如果您愿意,可以在https://github.com/airbr/whatsgood深入了解存储库。
解决方案
解决方案是将 Mongoose 升级到 5.0 版。
此外,我必须在这个答案中添加 url 解析器提及:
推荐阅读
- angular - Angular - *ngFor:从 ts 文件中的索引传递 i
- outlook-addin - 如何在 Safari 中为 Outlook 加载项对话框 API 启用开发人员控制台?
- ios - 在 Swift 中导航时,TextField 会卸载值
- authentication - 多个身份验证提供程序和多个入口点
- compiler-errors - 在程序上使用 llvm 传递时,错误:无法执行命令:分段错误(核心转储)
- ios - 如何在 UITextView textViewDidBeginEditing 中显示键盘后显示警报
- asp-classic - <% Response.ContentType = "application/vnd.ms-excel"%> 只下载ASP文件
- delphi - 如何使用 FireMonkey 进行倾斜补偿磁航向?
- c# - 运行 MSTest 时出现“找不到指定的文件”
- node.js - MERN 脚手架授权