Wednesday 24 May 2017

multiple queries one after another using async in node js

get forloop reult in array and used async async.forEach for getting multiple queries result, but result changes its position after refreshing the page. here is my code

 for(var k=0; k < mdmid_array_implode_array_st.length; k++){
                    var mdmid_arr=mdmid_array_implode_array_st[k];
                    sql="select sum(cnt_deal_amt) as cnt_deal_amt from crm_clients_feedback_log where crm_clients_feedback_log.cnt_mdm_id IN ("+mdmid_arr+")";
                    sql_dealwotx="select sum(cnt_deal_amt_widout_tax) as cnt_deal_amt_widout_tax from crm_clients_feedback_log where crm_clients_feedback_log.cnt_mdm_id IN ("+mdmid_arr+")";               
                    sql_query.push(sql);
                    sql_dealwotx_query.push(sql_dealwotx);
                }
                sql_query_array=sql_query;              
                sql_dealwotx_query_arr=sql_dealwotx_query;
                array=[];
                async.forEach(sql_query_array, function(query, callback1) {
                    pool.query(query,function(dealerr,dealrslt,next){
                        for(var k=0; k < dealrslt.length; k++){
                            dealrslt_sa=dealrslt[k].cnt_deal_amt;
                            if(dealrslt_sa){
                                dealrslt_sa_val=dealrslt_sa;
                            }
                            else{
                                dealrslt_sa_val=0;
                            }
                            dealrslt_sa_array.push(dealrslt_sa_val);
                    }
                        deals_done=dealrslt_sa_array;
                        dealrslt_sa_array=[];   
                        array.push(deals_done);
                        callback1(); // this signals async that you're done with this item
                     });
                }, function(err) {
                    console.log("array"+array);
                    if (err) return next(err);
                    async.forEach(sql_dealwotx_query_arr, function(queryde, callback2) {     
                    pool.query(queryde,function(dealerr,dealrsltwotx,next){
                        for(var k=0; k < dealrsltwotx.length; k++){
                            dealrslt_sa=dealrsltwotx[k].cnt_deal_amt_widout_tax;
                            if(dealrslt_sa){
                                dealrsltrx_sa_val=dealrslt_sa;
                            }
                            else{
                                dealrsltrx_sa_val=0;
                            }
                            dealrsltwotx_sa_array.push(dealrsltrx_sa_val);
                    }
                        deals_donewotx=dealrsltwotx_sa_array;
                        dealrsltwotx_sa_array=[];   
                        dealrsltwotx_array.push(deals_donewotx);
                        callback2();
                     });
                },function(err){                                        
                    res.render("salescharts",{citirslt:citirslt,deals_done_aa:array,dealrsltwotx_array:dealrsltwotx_array});
                });
                });
            }); 
            }); 

output:

    array 512529,0,0,0,0,1000,0,43600,0,9500,33000,0,0,0,0,11500,0,0,0,0,0,0

  dealrsltwotx_array 312881,0,0,0,0,14956,0,0,0,28696,0,0,0,0,2500,10000,0,0,0,0,0,0

after refreshing the page values has been changed

array 512529,0,0,0,0,43600,0,0,9500,33000,0,0,0,0,1000,11500,0,0,0,0,0,0
dealrsltwotx_array 2500,312881,0,0,0,0,14956,0,0,0,28696,0,0,0,0,10000,0,0,0,0,0,0



via tanu

No comments:

Post a Comment