Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

70
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda