-1

Well when i try to query, all is going good but when i try to store my result in the table my table is always stay empty but if i try to console.log(table); after the query i can see my table with the value of result. I don't really know why that doing this.

var addToList = [];
mysqlPool.getConnection(function(err, con){ 
  if(err) throw err;
  var requestSQL = "SELECT * FROM quests WHERE quest_id = ?";
  con.query(requestSQL, ["1"], function(err, result){
    if(err){
        console.log(err);
        con.release();
        return;
    }
    else{
        addToList.push(result[0]["quest_id"]);
    }
  });
});

console.log(addToList);
mscdex
  • 104,356
  • 15
  • 192
  • 153

1 Answers1

0

This is due to the asynchronous nature of node. getConnection() returns immediately and then console.log(addToList) is executed, which will display an empty array in the console. Sometime after that your getConnection() callback is executed.

mscdex
  • 104,356
  • 15
  • 192
  • 153