Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

76
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda