Reputation:
i am new in node js i stuck in calling multiple callback in waterfall of async method.
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);
});
}
}
]);
in the above code if the done(err, reserror); is call after done(err, 'debug1');.it does not wait for reserror so i want to check the error first if the reserror is not null or blank then only call done(err, 'debug1'); otherwise call the done(err, reserror);.please help me to findout the solution.thanks to all in advance.
Upvotes: 0
Views: 412
Reputation: 195
try below code.
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);
});
}
}
]);
Upvotes: 0
Reputation: 4565
You should use different name for error. Let say that below service error name is err1 and the top service error name is 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);
}
}
});
Upvotes: 0