我尝试从 GET
请求调用我的聚合函数,但响应为空。
有人可以帮我解决这个问题吗?这是我的代码:
聚合函数:
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});
});
然后查看是否收到响应或者是否看到控制台错误。 如果还是不行,那就是路由问题了
关于node.js - 使用 GET 请求调用聚合函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55901631/