I am trying to figure out how to use a function to return choices in an inquirer prompt but I can't seem to find any examples or explanations on how to do this. I have a MySQL2 query:
db.query(`SELECT id AS value, name FROM departments`, (req, res) => {
return Object.values(JSON.parse(JSON.stringify(res)))
})
returning an array of objects:
[
{ value: 1, name: 'Sales' },
{ value: 2, name: 'Engineering' },
{ value: 3, name: 'Finance' },
{ value: 4, name: 'Legal' },
{ value: 5, name: 'Human Resources' }
]
which seems like the returned array would fulfill what I need in my prompt:
{
type: 'list',
name: 'department',
message: 'In which department is this role?',
choices: [// CODE HERE]
}
but I keep getting 'undefined'.
I have tried to put the MySQL query straight into the choices array.
I have tried return
as well as console.log
and map
.
I have tried putting the query in a function and then calling the function within the choices prompt.
I have also confirmed that hardcoding my array of objects into the choices prompt does print successfully, but I don't want to hard code as other questions in the prompt allow the client to add to the departments table and I want that to be reflected in the choices.
What am I missing?
consider that when using async operation in choices function, you must return a promise