Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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 从mongoDB嵌套查询返回数据_Node.js_Mongodb_Express - Fatal编程技术网

Node.js 从mongoDB嵌套查询返回数据

Node.js 从mongoDB嵌套查询返回数据,node.js,mongodb,express,Node.js,Mongodb,Express,从嵌套查询返回数据的方法是什么 正在将多个函数调用合并到一个模块中, 我有以下代码 retrieveStudentSessions: function(req, res, callback){ MongoClient.connect(config.mongoPath+config.dbName, function(err, db) { if(err){ return callback(new Error("Unable to Connect to D

从嵌套查询返回数据的方法是什么

正在将多个函数调用合并到一个模块中, 我有以下代码

  retrieveStudentSessions: function(req, res, callback){
     MongoClient.connect(config.mongoPath+config.dbName, function(err, db) {
       if(err){
         return callback(new Error("Unable to Connect to DB"));
       }
      var collection        = db.collection(config.userList);
      var sessionCollection = db.collection(config.session);
      var templateCollection = db.collection(config.template);
      var tempSession  = {session:[], templates:[]};

      //Obtain UserID
      collection.find({'email' : req.user['_json'].email}).nextObject(function(err, doc) {
          if(err){
                return callback(new Error("Error finding user in DB"));
          } 
          //Search Session collection for userID
            sessionCollection.find({ $or : [{'studentsB' : doc['userid']},{'studentsA' : doc['userid']}]}).each(function(err, doc) {
              if(err){
                  return callback(new Error("Error finding user in DB"));
              }  
                  //Update JSON
                  tempSession.session.push(doc);
                  //Query for Template Title using Template ID from Session Collection
                  templateCollection.find({'_id' : doc['templateId']}).nextObject(function(err, doc){
                    if(err){
                          return callback(new Error("Error finding user in DB"));
                    } 
                    //Update JSON
                    tempSession.templates.push(doc);
                  });
            });
        return callback(null, tempSession);
      });
    });
  }
调用函数

dbCall.retrieveStudentSessions(req, res, function(err, result){
                if(err){
                    console.log(err);
                    return;
                }
                console.log(result);
            });

当我尝试返回变量
tempSession
时,上面的代码返回error
undefined不是函数。这同样适用于单个查询。对于嵌套查询,是否有特定的返回方法

这里的回调是什么?这看起来像是一个请求处理程序?您是否得到了包含该错误的堆栈跟踪?如果是,它指向哪里?另外,您的代码应该使用类似的内容,因为您在
sessionCollection.find()之前调用回调。each()
已经完成。错误总是指向
返回回调(null,tempSession)。我尝试在sessionCollectionfind之后以及templateSessionCollection之后使用回调。数据生成正确。但是当包含return语句时,
undefined不是一个函数