我正在尝试从A GET
请求调用我的聚合函数,但响应是EMPTY。
有人可以帮我弄这个吗?这是我的代码:
聚合功能:
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});
});
然后查看您是否收到响应或是否看到控制台错误。如果你还没有,那么这是一个路由问题