Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
Empty array after a for loop with sql queries

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)
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!