I am trying to fetch records from mongoose in loop. But it is not working as expected. I have an array of hashes with questions and answers and I'm trying to find those questions from my db. Here is my loop:
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)
quiz.where("question",question_to_find).exec(function(err,results)
{
console.log(results)
if (ans == "t")
{
user_type = results.t
}
else if (ans == "f")
{
user_type=results.f
}
})
}
and result from terminal is something like:
0
t
1
f
[ { question: 'i was here',
_id: 5301da79e8e45c8e1e7027b0,
__v: 0,
f: [ 'E', 'N', 'F' ],
t: [ 'E', 'N', 'F' ] } ]
[ { question: 'WHo r u ',
_id: 5301c6db22618cbc1602afc3,
__v: 0,
f: [ 'E', 'N', 'F' ],
t: [ 'E', 'N', 'F' ] } ]
The problem is that my questions are displaying after loop's iteration. And because of that I am unable to process them.
Please help!
Regards
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…