Home  >  Q&A  >  body text

In Node.js, Inquirer exits before I make a selection

I'm trying to write a command line application that uses an interrogator to display and ultimately update a mysql database. When I use the mysql shell my database files seem to be in order, but when I try to connect to my database and manipulate it through the queryer I seem to have issues.

So far it will log that it is connected to the correct database, display the menu options, and then immediately exit without allowing me to make a selection.

If I comment out the const connect block, the queryer will not exit and allow me to make a selection, but then the application will break because there is no connection to the database.

Thanks in advance for any insights

My current code:

require('console.table');
const inquirer = require ('inquirer');
const mysql = require ('mysql2');

const connect = mysql.createConnection(
    {
        host: 'localhost',
        user: 'root',
        password: '',
        database: 'employeeDB'
    },
    console.log('Connected to employeeDB')
);

function init(){
    menu();
};

async function menu(){
    await inquirer.prompt([
            {
                type: "list",
                name: "userChoice",
                message:"Menu:",
                choices: [
                    "View All Departments",
                    "View All Roles",
                    "View All Employees"
                ]
            },
        ])
        .then(({userChoice}) => {
            if (userChoice === "View All Departments"){
                viewDepartment()
            } else if (role === "View All Roles") {
                viewRole()
            } else {
                viewEmployee()
            }
        })
}

const viewDepartment = () => {
    connect.query(
        'SELECT * FROM department;',
        (err, results) => {
            console.table(results);
            menu();
        }
    )
};

const viewRole = () => {
    connect.query(
        'SELECT * FROM role;',
        (err, results) => {
            console.table(results);
            menu();
        }
    )
};

const viewEmployee = () => {
    connect.query(
        'SELECT * FROM employee;',
        (err, results) => {
            console.table(results);
            menu();
        }
    )
};

init();

P粉947296325P粉947296325259 days ago296

reply all(1)I'll reply

  • P粉676588738

    P粉6765887382024-02-04 00:00:54

    I think your msql connection is causing the problem. You did not enter a password, thus causing the crash. But it happens asynchronously, so the interrogator prompt will appear anyway, but your app will crash before you make your selection

    reply
    0
  • Cancelreply