Wednesday 24 May 2017

res.send() execute before the functionality code executes in node js

I am new to node js. In my app, I am querying the MySql DB and process and view the processed results using node js. If my query returns null data then I do another query and do the process. But my res.send(finaldate) code gets executed before the functional process gets complete. How to solve this. res.send(finaldata) executes first before the "do require stuff for finaldata" ;

var finaldata ={};
//first query
var result = connection.query('myquery', function(err, data_rows){
if(!err){
  //second
  //doing one more Query for some other records from DB (second query)
  connection.query('myquery', function(err, rows, fields){
   if(0 < rows.length){
     finaldata = //do require stuff for finaldata 
   }
   else{
      //third
      //do the second query with some condition
      connection.query(queryText, function(err, rows, fields) {
            finaldata = //do require stuff for finaldata 
      })
  }
 res.send(finaldata);

  });
 }

});



via Sami

No comments:

Post a Comment