In this code what I do is, for each person (first and last name) that I have inside the dbres2['people'] array, look up their Id in the db.
What I don't understand is how to store row.id inside my array array_ids. Because when I print the contents with the last console.log the array appears empty. What I have read on the internet is that js sql queries happen asynchronously and so the value of array_ids is perhaps printed before it is even filled with values. How can I make sure that this array is printed only after the for loop with the various sql queries is finished?
let array_ids = []
for(i in dbres2['people']){
const p = (dbres2['people'][i]).split(" ");
let name = p[0];
let surname = p[1];
db.all('SELECT id from people WHERE name = ? and surname = ?', [name, surname], (err, row) => {
if(err) {
return console.log(err.message);
}
else{
array_ids.push(row.id)
}
})
}
console.log(array_ids)