[英]multiple callback in the async in node js
我是節點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);
});
}
}
]);
在上面的代碼中,如果done(err,reserror); 完成后被調用(err,'debug1'); .it不等待重新錯誤,所以如果重新錯誤不為null或為空,我想先檢查錯誤,然后僅調用done(err,'debug1'); 否則調用done(err,reserror); 請幫助我找出解決方案。謝謝所有。
您應該為錯誤使用其他名稱。 假設下面的服務錯誤名稱為err1,最上面的服務錯誤名稱為err
Offer.find({ merchant_id:req.body.merchant_id,loyalty_offer:true })
.populate('merchant_id').exec(function(err1, loyaltyoff) {
if(loyaltyoff.length>0){
if(loyaltyoff.length===
offerscount[0].merchant_id.more_details.fields.subscription.loyalty_offers){
console.log('hello');
reserror = 'Exceeds the loyalty offers limit.';
if (reserror !== null && reserror !== undefined) {
done(err, 'debug1');
} else {
done(err1, reserror);
}
}
});
嘗試下面的代碼。
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('inside loyalty');
reserror = {
"status":0,
"data":"",
"message":"Exceeds the loyalty offers limit."
};
// reserror = 'Exceeds the loyalty offers limit.';
done(err, reserror);
//return res.json(reserror);
//next();
}else{
done(err, 'debug1');
}
}else{
done(err, 'debug1');
}
});
}else{
done(err, 'debug1');
}
}else{
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.'
};
return 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);
});
}
}
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.