我正在尝试从循环中的mongoose中获取记录。但它没有按预期工作。我有一系列带有问题和答案的哈希,我正试图从我的数据库中找到这些问题。这是我的循环:
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
}
})
}
终端的结果如下:
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' ] } ]
问题是我的问题是在循环迭代后显示的。因此,我无法处理它们。
请帮忙! 此致
答案 0 :(得分:17)
欢迎来到async-land: - )
使用JavaScript除了代码之外,任何事情都会并行发生。这意味着在您的特定情况下,在循环结束之前无法调用回调。您有两种选择:
a)将同步for循环的循环重写为异步递归循环:
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
});
此外,我建议研究this blog。它包含了async-loop-stairway的两个进一步的步骤。非常有帮助且非常重要。
b)将异步函数调用放入格式为
的闭包中(function( ans ) {})(ans);
并为其提供您要保留的变量(此处为: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);
}
答案 1 :(得分:0)
当您运行 mainFunction()
时,该函数调用 getData()
const mainFunction = async()=>{
answers
console.log("start mainFunction")
resultFromFindLoob = await getData(answers)
console.log("finish mainFunction");
console.log("final result:" , resultFromFindLoob)
}
mainFunction();
getData()
函数是一个promise 循环函数,当函数完成时,返回所有数据,您可以在mainFunction()
let async = require('async');
let q = require('q');
const getData = async (answers)=>{
console.log("start getData")
let defer =q.defer();
let result = []
async.eachSeries(answers , async(asnwer)=>{
try {
let data = await MyModel.find(asnwer).lean();
//do something on data (processing)
result.push(data)
} catch (error) {
console.log(error)
}
},()=>{
console.log("finish find loop getData")
defer.resolve(result)
})
return defer.promise
}
mainFunction() 基于 console.log 的结果
1.start mainFunction
2.start getData
3.finish find loop getData
4.finish mainFunction
5.final result : .....