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
时,上面的代码返回errorundefined不是函数。这同样适用于单个查询。对于嵌套查询,是否有特定的返回方法 这里的回调是什么?这看起来像是一个请求处理程序?您是否得到了包含该错误的堆栈跟踪?如果是,它指向哪里?另外,您的代码应该使用类似的内容,因为您在sessionCollection.find()之前调用回调。each()
已经完成。错误总是指向返回回调(null,tempSession)代码>。我尝试在sessionCollectionfind之后以及templateSessionCollection之后使用回调。数据生成正确。但是当包含return语句时,undefined不是一个函数
。