私はノードjs iで新しいです。非同期メソッドのウォーターフォールで複数のコールバックを呼び出すことに悩まされました。上記のコードでノードjsの非同期で複数のコールバック
var offer = new Offer(req.body);
offer.featured_tag=false;
var err = '';
reserror='';
async.waterfall([
function (done) {
if(req.body.create_role === 'Merchant' || req.body.create_role=== 'SubMerchant'){
//if offer created by merchant is less than than the subscription of merchant then active this offer when adding otherwise deactive
offer.active_immediately=false;
Offer.find({ merchant_id:req.body.merchant_id }).populate('merchant_id').exec(function(err, offerscount) {
// count no of offers createdBy merchant
console.log(offerscount);
var noofrecords=offerscount.length;
if(noofrecords>0){
if(typeof offerscount[0].merchant_id.more_details.fields!=='undefined'){
if(offerscount[0].merchant_id.more_details.fields.subscription){
if(noofrecords<offerscount[0].merchant_id.more_details.fields.subscription.number_offer){
offer.active_immediately=true;
}
if(offerscount[0].merchant_id.more_details.fields.subscription.feature_tag === true){
offer.featured_tag=true;
}
}
if(req.body.loyalty_offer==true){
Offer.find({ merchant_id:req.body.merchant_id,loyalty_offer:true }).populate('merchant_id').exec(function(err, loyaltyoff) {
console.log('count:'+loyaltyoff.length);
if(loyaltyoff.length>0){
if(loyaltyoff.length===offerscount[0].merchant_id.more_details.fields.subscription.loyalty_offers){
console.log('hello');
/* reserror = {
"status":0,
"data":"",
"message":"Exceeds the loyalty offers limit."
};*/
reserror = 'Exceeds the loyalty offers limit.';
done(err, reserror);
}
}
});
}
}
done(err, 'debug1');
}
}else if(req.body.create_role === 'Admin'){
done(null,'debug1')
}
}, function(err, reserror) {
console.log('load');
var startdate = new Date(req.body.startdate);
offer.startdate = startdate.toISOString();
var enddate = new Date(req.body.enddate);
offer.enddate = enddate.toISOString();
offer.createdOn=Date.now();
offer.createdBy=req.body.creater_id;
offer.isDeleted= false;
offer.offer_image=req.body.image;
console.log('bug'+err);
if(err!='debug1'){
var reserror1 = {
"status":0,
"data":"",
"message":'Exceeds the loyalty offers limit.'
};
res.json(reserror1);
}else{
offer.save(function(err,data) {
if (err) {
response = {
"status":0,
"error":err
};
}else{
Category.findById(req.body.main_cat, function (err, catdataset) {
var offerset = {
offer_id: data._id,
posted_by: data.createdBy,
datetime: data.createdOn
};
catdataset.offers.push(offerset);
catdataset.save();
});
response = {
"status":1,
"data":data,
"message":"Offer has been created."
};
}
console.log(response);
res.json(response);
});
}
}
]);
場合完了(ERR、reserror)。は、完了後にコールされます(err、 'debug1'); .itはreserrorを待っていないので、reserrorがnullでも空白でもない場合はエラーを最初にチェックしたいのでdone(err、 'debug1');それ以外の場合はとしてください(エラー、再エラー)。。私は解決策を見つけるのを手伝ってください。
助けていただきありがとうございます。 –
ああ。しかし、あなたは "reserrorが空でないか空白でなくてもdone(err、 'debug1')しか呼び出さない場合はエラーを最初にチェックしたいと言っています;そうでなければdone(err、reserror)を呼び出す" – digit
loyaltyoff.lengthの状態をチェックします。それがtrueであれば、done(err1、reserror)を呼び出します。私のコードを見ると、loyaltyoff.lengthが0より大きくないという考えが得られます。 –