FindOneの非同期に問題があります。私はidChildren([516152]、[158796]、[123654,147852])のようなidの配列を持っています)、配列上の現在の位置のidがデータベース上にあるかどうかをチェックしたいと思います。 childrenは、すべての情報で文字列を保存する配列です。FindOneのAsync.eachSeriesが機能しない
for (var j = 0; j < idChildren.length; j++)
{
async.eachSeries(idChildren, function() {
for (var i = 0; i < idChildren[j].length; i++)
{
Children.findOne({ _id: idChildren[j] }, function (err, child) {
console.log("I'm in the findOne");
if (child != undefined && !err) {
if (child.lastname == undefined)
tmp2 += ',';
else
tmp2 += child.lastname + ',';
if (child.firstname == undefined)
tmp2 += ',';
else
tmp2 += child.firstname + ',';
if (child.birthday == undefined)
tmp2 += ',';
else
tmp2 += child.birthday + ',';
tmp2 += '\n';
}
children.push(tmp2);
tmp2 = "";
});
}
});
}
console.log(children);
"console.log(子)" print [、、]と "私はfindOneにいる"の直後に3回問題があります。私はasync.eachSeriesが正しくないと思う。 私を助けることができますか?
EDIT
async.eachSeries(idChildren, function(idChild, next) {
Children.findOne({ _id: idChild }, function (err, child) {
console.log("I'm in the findOne");
if (child != undefined && !err) {
if (child.lastname == undefined)
tmp2 += ',';
else
tmp2 += child.lastname + ',';
if (child.firstname == undefined)
tmp2 += ',';
else
tmp2 += child.firstname + ',';
if (child.birthday == undefined)
tmp2 += ',';
else
tmp2 += child.birthday + ',';
tmp2 += '\n';
}
children.push(tmp2);
tmp2 = "";
next();
return;
});
});
console.log(children);
出力は次のとおりです。
[]
私は私がfindOneによfindOne
によfindOneに
よ
私はfindOneにいます
私はfindOneにいます
したがって、console.log(子)が最初に実行され、FindOneループの直後に実行されます。 findOneの最後にconsole.log(子)がある場合、配列は正しいです。
私は私の答えを編集しました –