Trying to use a promisified function to insert values into an inquirer choices field

27 views Asked by At

I set up the promise function via:


const mysql = require('mysql2');
const util = require('util');
require('dotenv').config();


const db = mysql.createConnection(
    {
        host: 'localhost',
        user: process.env.DB_USER,
        password: process.env.DB_PASSWORD,
        database: process.env.DB_NAME
        

    },

    console.log(`Connected to the employeetracker_db database.`)
);

const query = util.promisify(db.query).bind(db);

I want to select all job roles in my database and then input it into my inquirer choices field for a question via:

const getAllRoles = async () => {
    await query(`SELECT title AS 'Job Title' FROM role;`, (err,results) => {
        if (err) {
            console.log(err);
        } else {
            results.map(function(results) {
            return results['Job Title']});
           };
        });
    };
    
const roles = getAllRoles().then((results)=> {
    return results;
});
console.log(roles);

However, no matter what permutation of async/await, synchronous functions, and .then() statements I've tried, I'm getting either undefined (if I don't promisify this) or Promise {<pending>}. I've tried using .then with a "return results" cb function on roles, I've tried adding await to the .map cb function. How do I get the promise to resolve and get my data?

0

There are 0 answers