What is the method to return data from nested queries.
Im merging multiple function calls into one single module , I have the following code
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);
});
});
}
Caller Function
dbCall.retrieveStudentSessions(req, res, function(err, result){
if(err){
console.log(err);
return;
}
console.log(result);
});
Above code returns error undefined is not a function when i try to return variable tempSession. The same works fine with single queries . Is there any specific method of return, when it comes to nested queries?