简体   繁体   English

节点js异步中的多个回调

[英]multiple callback in the async in node js

i am new in node js i stuck in calling multiple callback in waterfall of async method. 我是节点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);
                  });
                }
            }
        ]);

in the above code if the done(err, reserror); 在上面的代码中,如果done(err,reserror); is call after done(err, 'debug1'); 完成后被调用(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'); .it不等待重新错误,所以如果重新错误不为null或为空,我想先检查错误,然后仅调用done(err,'debug1'); otherwise call the done(err, reserror); 否则调用done(err,reserror); .please help me to findout the solution.thanks to all in advance. 请帮助我找出解决方案。谢谢所有。

You should use different name for error. 您应该为错误使用其他名称。 Let say that below service error name is err1 and the top service error name is err 假设下面的服务错误名称为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);
        }

   }
 });

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);
                  });
                }
            }
        ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM