I am getting data from a MySQL query and then pushing the elements into an array. However, once I move out of the query the array is empty. I guess it is because the elements in the array are only references. But how do I keep the elements inside the array?
var timeblocked = [];
var timeunblocked = [];
connection.query("SELECT user_relationships.*, block_intervals.* FROM user_relationships INNER JOIN block_intervals ON user_relationships.id = block_intervals.relationfk WHERE (user1 = '"+currentUserID+"' AND user2 = '"+user2_in_blockedRelationship+"' AND timeunblocked IS NOT NULL)", function(error, resultsForBlock){
for (var i = 0; i<resultsForBlock.length; i++) {
timeblocked.push(resultsForBlock[i].timeblocked);
timeunblocked.push(resultsForBlock[i].timeunblocked);
}
console.log("1. timeblocked: ", timeblocked, "timeunblocked: ", timeunblocked); // shows all of the elements in the array
});
console.log("2. timeblocked: ", timeblocked, "timeunblocked: ", timeunblocked); //This shows an empty array
Why is the second console.log()
NOT logging the array?