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

112
Visualizações
How can I dynamically populate inquirer choices array with sql data?

I have a nodejs project that uses inquirer as well as mysql2. I created a class to store my sql queries as functions to be able to use those with inquirer. When I run the following function, I get undefined when I am returning the managers array. But when I console.log the array, everything displays as expected. This is my function:

    viewManagers() {
        const sql = `SELECT CONCAT(first_name, ' ',  last_name) AS manager FROM employee WHERE manager_id IS NULL;`;
        
        db.query(sql, (err, rows) => {
            if (err) throw err;
            const managers = [];
           
           for (let i = 0; i < rows.length; i++) {
               managers.push({ name: rows[i].manager, value: i+1 });
           }
           managers.push({ name: "None", value: null });
           return managers;
        });
        
    }

I am calling it like this to test it,

const query = new Query();
console.log(query);

If I change the return statment in the viewManagers function to a console.log(managers); Then the managers array is returned in the console. How can I successfully return this data so it is not undefined? I want to be able to return it and then call this function from within another function. I also tried using promisify and got the same result:

const db = require('../db/connection');
const util = require('util');
const query = util.promisify(db.query).bind(db);

class Query {
async viewManagers() {
        try {
            const sql = `SELECT CONCAT(first_name, ' ',  last_name) AS manager FROM employee WHERE manager_id IS NULL;`;
            const rows = await query(sql);
            const managers = [];
            for (let i = 0; i < rows.length; i++) {
               managers.push({ name: rows[i].manager, value: i+1 });
           }
           
           managers.push({ name: "None", value: null });
           return managers;

        } finally {
            db.end();
        }
 
    }
}

module.exports = Query;
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