0

I have the following code:

var data = 0;
connectionSelectPackageId.connect();

  let sqlSelectPackageId = "SELECT MAX(packageId) as maxId FROM recipients";

    connectionSelectPackageId.query(sqlSelectPackageId, function(err, rows){
  if(err) {
    throw err;
  } else {

      for(var i=0; i< rows.length; i++) {

        data = data + rows[i].maxId;
        console.log(data);
  };
  };
});
    connectionSelectPackageId.end();

console.log(data);

The issue is that console.log(data) get executed before the query has completed, and hence its value is the default which is zero instead of taking into account the added value in the query.

I would like to use the data variable in subsequent code. Hence, my question is how can i run the remaining portion of my code only after this query has been completed? The variable needs to work outside of the scope of the query.

The follow up query

connectionInsert.connect();

 // Insert into the recipients the hashtag and information needed to opon the lock

function getDateTime() {
    var now     = new Date(); 
    var year    = now.getFullYear();
    var month   = now.getMonth()+1; 
    var day     = now.getDate();
    var hour    = now.getHours();
    var minute  = now.getMinutes();
    var second  = now.getSeconds(); 
    if(month.toString().length == 1) {
        var month = '0'+month;
    }
    if(day.toString().length == 1) {
        var day = '0'+day;
    }   
    if(hour.toString().length == 1) {
        var hour = '0'+hour;
    }
    if(minute.toString().length == 1) {
        var minute = '0'+minute;
    }
    if(second.toString().length == 1) {
        var second = '0'+second;
    }   
    var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second;   
     return dateTime;
}

var dateTime = getDateTime();

  // Add email later on
  let sqlInsert = 'INSERT INTO recipients (date, ..., ..., ..., ..., ..., ..., data) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';

   connectionInsert.query(sqlInsert, [dateTime, ..., ..., ...,...,..., ..., data], function(err, rows, fields) {
  });

connectionInsert.end();
tadman
  • 208,517
  • 23
  • 234
  • 262
code_legend
  • 3,547
  • 15
  • 51
  • 95
  • The `function (err, rows)` is how you specify code to run after the query has completed. Use `console.log(data)` inside that function's body. Detailed explanation: [Why is my variable unaltered after I modify it inside of a function?](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Jonathan Lonowski Dec 03 '16 at 02:56
  • I was going to mark this as a duplicate of http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call - however that question invovles jQuery which may be confusing - however, the general idea of that question remains – Jaromanda X Dec 03 '16 at 02:57
  • thanks but i would want to use the variable outside of the scope of the query. Such as i can insert that variable into as a value into another table – code_legend Dec 03 '16 at 02:57
  • 1
    if you are using jquery then you should try `jQuery.when()` [link](https://api.jquery.com/jquery.when/) – geekbro Dec 03 '16 at 03:05
  • Thanks i have updated my code to shows up the second query that would run afterwards and where the data value collected from the first query would be inserted to – code_legend Dec 03 '16 at 03:09
  • Not sure why this was tagged `mysqli` which is a PHP thing. – tadman Dec 03 '16 at 03:27
  • 1
    Writing async database code like this can be considerably easier with a layer like [Sequelize](http://sequelizejs.com). Then you can use promises to properly order your code. – tadman Dec 03 '16 at 03:27
  • Thank you. How would Sequelize be applied in this scenario? – code_legend Dec 03 '16 at 03:29

0 Answers0