My current code is not waiting for the loop to finish before printing {"personalchats":[]}
I need to send after the for loop has completed. How can I fix this issue?
connection.query("SELECT * FROM personalchat WHERE user1ID = ? OR user2ID = ?", [userID, userID], function(err, row, fields) {
if (err)
console.log(err);
else {
personalchats = Array();
for (var i = 0; i < row.length; i++) {
if (row[i].user1ID != userID)
user2ID = row[i].user1ID;
else
user2ID = row[i].user2ID;
connection.query("SELECT * FROM users WHERE userID = ?", [user2ID], function(err2, row2, fields2) {
if (err)
console.log(err2);
else {
personalchats.push({
'success': true,
'userID': row2[0].userID,
'name': row2[0].name,
'surname': row2[0].surname,
'email': row2[0].email
});
console.log(personalchats);
}
});
}
res.send({
"personalchats": personalchats
});
}
});