0

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();
aaburz
  • 1
  • 1
  • If Inquirer exits before you are able to make a choice, then it isn't mysql. It is likely that your terminal doesn't like Inquirer, have you tried using a different terminal? – Jonathan Rosa Mar 19 '22 at 01:37
  • 1
    Thanks for the response! Yes, I attempted in git bash as well as the built in terminal in VC code, I have the same issue. If I comment out the block of code that connects to mysql then inquirer will prompt and allow me to make a selection, but then of course everything breaks as its not connected to a DB – aaburz Mar 19 '22 at 02:05

1 Answers1

0

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