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;
}