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

111
Views
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
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!