Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用GET请求调用聚合函数_Node.js_Mongodb_Mongoose_Aggregate - Fatal编程技术网

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函数。我看不出上述代码有任何错误。您可能在错误的位置或错误的文件中检查。你好,尼姆,非常感谢您的回复!这正是路由问题。