I am new in MongoDb and NodeJs, I am using Mongoose in nodejs, But I am facing a problem, I am using result object of first query into nested query but getting undefined of first query object. please give me any suggestion. Thanks.
LuckyLunch.find(criteria, function (err, lunches) { // First Query
if (err)
return;
var allLunches = lunches; // Accurate result here
for (var i = 0; i < allLunches.length; i++)
{
if (typeof allLunches[i].luckyMatch != 'undefined') {
var usernames = [];
Match.findOne(criteria2, function (err, singleMatch) { // Second Query
if (singleMatch != null) {
var userids = [];
for (var k = 0; k < singleMatch.participants.length; k++)
{
userids.push(ObjectId(singleMatch.participants[k]));
}
User.find(criteria, function (err, getusers) { // Third Query
for (var j = 0; j < getusers.length; j++)
usernames.push(getusers[j].name);
allLunches[i].luckyUsers = usernames; // allLunches[i] is undefined here.
});
}
});
}
}
});