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

71
Views
Function returning empty array

I am using a function to return an array of names (variable name list in the code below) that will be used to supply the choices for an Inquirer.js prompt. This array ultimately comes from a MySQL database. The problem I am having is that the array I am returning from the function below is empty.

// return array of employee names
listEmployees() {
const list = [];
const sql = `SELECT CONCAT(first_name, ' ', last_name) AS name FROM employee`;
  db.query(sql, (err, rows) => {
    if (err) {
      console.error('Error.');
      return;
    }
    const arr = JSON.parse(JSON.stringify(rows));
    for (let i = 0; i < arr.length; i++) {
      list.push(arr[i].name);
    }
    console.log(list); // *** LIST IS DISPLAYING AS INTENDED HERE ***
  });
  console.log(list); // *** LIST IS EMPTY ARRAY HERE *** 
  return list;
}

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!