node.js - 使用 GET 请求调用聚合函数
问题描述
我正在尝试从 AGET
请求中调用我的聚合函数,但响应为空。
有人可以帮我弄这个吗?这是我的代码:
聚合函数:
function t1(callback) {
userScheme.aggregate([
// Unwind the array
{ "$unwind": "$result"},
// Group on the "_id" and "name" and $sum "value"
{ "$group": {
"_id": {
//"_id": "$_id",
"game": "$result.game"
},
"time": { "$avg": "$result.time" }
}},
// Put things into an array for "nice" processing
{ "$group": {
"_id": "$_id",
"values": { "$push": {
"game": "$_id.game",
"time": "$time"
}}
}}
] , callback)
}
我的GET
要求:
userRoutes.route('/getavg').get(function(req, res) {
t1(function(err, user) {
if (err)
res.status(500).send("Internal error occurred.");
else
res.json(user);
})
});
我做错了什么?
解决方案
代码看起来不错。
我建议您尝试简化您的代码。首先用类似的东西替换get函数
userRoutes.route('/getavg').get(function(req, res) {
console.error("Testing");
res.json({test: "works});
});
然后查看是否收到响应或是否看到控制台错误。如果还是不行那就是路由问题
推荐阅读
- reactjs - 在其他设备上直播 netlify 应用程序黑屏
- vb.net - iText 7签名字段对齐
- javascript - 如何在 node.js 中定义要观看的多种文件类型
- arcore - 我是否需要为 AR 应用程序开发安装 Google Play 服务
- jquery - JQuery on_click: removeClass in a button then toggleClass another (只有一个按钮有类)
- javascript - 我收到此错误 DiscordAPIError: Invalid Form Body
- typescript - 如何解决问题没有找到 exportAs 'matAutocomplete' 的指令?
- python - 我似乎无法让我的两只眼睛为 Turtle 工作
- ruby-on-rails - 返回已归档对象的所有关系的活动记录:false
- c# - 如何使用 post 方法在 ASP.net core MVC 中显示内容