0

I am trying ti use the results of a MySQL query to populate the choices of a inquirer prompt am having trouble getting the query results out into a variable.

I have the following in my code for a switch wit multiple actions that can be done to manipulate a db with employee, department, and role information.

I repeatably get undefined when I console.log out the the Object.keys(questions.roles) I get the following.

[
  '_events',       '_eventsCount',
  '_maxListeners', 'next',
  'sql',           'values',
  '_queryOptions', 'namedPlaceholders',
  'onResult',      'timeout',
  'queryTimeout',  '_fieldCount',
  '_rowParser',    '_fields',
  '_rows',         '_receivedFieldsCount',
  '_resultIndex',  '_localStream',
  '_unpipeStream', '_streamFactory',
  '_connection',   'options'
]

Then when I try to call any value I am getting undefined when I call questions.roles.values or any other attribute.

Even the console.logs in the callback aren't working either.

       case("13"): //  Delete a role
      questions.roles = db_conn.query("SELECT title, id FROM role;",
                      (err, results) => {
                    console.table(results);
                    questions.roles = results;
                      });
      console.log(Object.keys(questions.roles));
      console.log(questions.roles.values);
      
      let delRole = inquirer.prompt(questions.deleteRole);
      db_conn.query(queries.deleteRole,
            [delRole],
            (err,res) => console.log(`Deleted ${delRole}`));

      break;

I have tried to call multiple attributes that pop up when I enter Object.keys(questions.roles) and additional area of the callback in and out of it as well.

cwen13
  • 1

1 Answers1

0

The assignment questions.roles = db_conn.query(.....) is wrong. Just call:

db_conn.query("SELECT title, id FROM role;", (err, results) => {
   if (err) {
      ....
      return;
    }
    if (result.length)  {  // we have returned records
       let c
       for (c=0; c<result.length; c++) {
          console.log(result[c].title, result[c].id)
       } 
    }
});