async.eachは、非同期のLibによって提供される機能は3つのフィールド 1-コレクション/配列 2 - 反復 3-コールバックを持っている.IT非常に便利で強力です コレクションはオブジェクトの配列またはコレクションに参照され、反復は各反復を参照し、コールバックはオプションです。 コールバックを行っている場合は、フロントエンドに表示したい回答または結果を返します
関数collを各アイテムに並列に適用します。 iterateeはリストから項目を呼び出し、終了時のコールバックを呼び出します。 iterateeがそのコールバックにエラーを渡すと、(各関数の)メインコールバックがエラーとともに直ちに呼び出されます。
この関数は各項目にiterateeを並列に適用するので、iteratee関数が順番に完了するという保証はありません。
exapmle-
var updateEventCredit = function (userId, amount ,callback) {
async.each(userId, function(id, next) {
var incentiveData = new domain.incentive({
user_id:userId,
userName: id.userName,
amount: id.totalJeeneePrice,
description: id.description,
schemeType:id.schemeType
});
incentiveData.save(function (err, result) {
if (err) {
next(err);
} else {
domain.Events.findOneAndUpdate({
user_id: id.ids
}, {
$inc: {
eventsCredit: id.totalJeeneePrice
}
},{new:true}, function (err, result) {
if (err) {
Logger.info("Update status", err)
next(err);
} else {
Logger.info("Update status", result)
sendContributionNotification(id.ids,id.totalJeeneePrice);
next(null,null);
}
});
}
});
おかげで、私はそれが簡単な何かを知っていた@stewe! よろしく、ベン。 – Ben
上記のコードはすべての反復で 'callback'関数を呼び出しませんか? 'count ++;のようなものでなければならない。 if(dataObj.length == count)callback(); '新しい変数' var count = 1; 'は' async.forEach'ループを呼び出す前に宣言されています? –
ありがとうございました! –