Node mongoose find query in loop not working
Welcome to async-land :-)
With JavaScript anything happens in parallel except your code. This means in your specific case, that the callbacks cannot be invoked before your loop has ended. You have two options:
a) Rewrite your loop from a sync for-loop to an async recurse-loop:
function asyncLoop( i, callback ) {
if( i < answers.length ) {
console.log(i)
var question_ans = eval('(' + answers[i]+ ')');
var question_to_find = question_ans.question.toString()
var ans = question_ans.ans.toString()
console.log(ans)
quiz.where("question",question_to_find).exec(function(err,results) {
console.log(ans, results)
if (ans == "t") {
user_type = results.t
} else if (ans == "f") {
user_type=results.f
}
asyncLoop( i+1, callback );
})
} else {
callback();
}
}
asyncLoop( 0, function() {
// put the code that should happen after the loop here
});
Additionally I recommend the study of this blog. It contains two further steps up the async-loop-stairway. Very helpful and very important.
b) Put your async function call into a closure with format
(function( ans ) {})(ans);
and provide it with the variable you want to keep (here: ans
):
for (var i=0;i < answers.length;i++) {
console.log(i)
var question_ans = eval('(' + answers[i]+ ')');
var question_to_find = question_ans.question.toString()
var ans = question_ans.ans.toString()
console.log(ans)
(function( ans ) {
quiz.where("question",question_to_find).exec(function(err,results) {
console.log(ans, results)
if (ans == "t") {
user_type = results.t
} else if (ans == "f") {
user_type=results.f
}
})
})(ans);
}