Inquirer exits before I am able to make a choice in node js

788 views Asked by At

I am attempting to write a command line app that uses inquirer to display and eventually update a mysql database. My db files appear to be in order when I use the mysql shell, however I appear to run into an issue when I attempt to connect to my db and manipulate it through inquirer.

As of now it will log that it has connected to the correct database, show the menu options then promptly exit without allowing me to make a selection.

If I comment out the const connect block of code then inquirer will not exit and allow me to make a selection, but then the app breaks as there is no DB connected.

Thanks in advance for any insight

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();
1

There are 1 answers

0
Alex Sigala On

I think your msql connection is causing the problem. you didnt put a password so its causing a crash. It happens asynch though so the inquirer prompt happens anyway bt your app crashes before you can make the selection