Node.js 使用GET请求调用聚合函数
我试图从Node.js 使用GET请求调用聚合函数,node.js,mongodb,mongoose,aggregate,Node.js,Mongodb,Mongoose,Aggregate,我试图从GET请求调用聚合函数,但响应为空 有人能帮我吗?这是我的密码: 聚合函数: function t1(callback) { userScheme.aggregate([ // Unwind the array { "$unwind": "$result"}, // Group on the "_id" and "name" and $sum "value" { "$group": { "_id": { //"_id": "$_id", "ga
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)
}
userRoutes.route('/getavg').get(function(req, res) {
t1(function(err, user) {
if (err)
res.status(500).send("Internal error occurred.");
else
res.json(user);
})
});
我的获取请求:
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)
}
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});
});
然后查看是否得到响应或是否看到控制台错误。
如果您仍然不这样做,那么这是一个路由问题1)从聚合管道中删除所有阶段,看看您得到了什么。2) 您正以一种旧式的方式生活(
回调
)。尝试使用async
wait
使用新功能当我在聚合函数中打印响应时,我得到了数据,因此我认为问题在于我的GET函数。我看不出上述代码有任何错误。您可能在错误的位置或错误的文件中检查。你好,尼姆,非常感谢您的回复!这正是路由问题。