简体   繁体   中英

Why are multiple inquirer prompts running concurrently even outside of the method they are in?

I am attempting to create a command line interface that prompts users to enter or retrieve data from a database.

So far the only methods I have written are for the info needed from the user to create new database rows. However when one of the inquirer prompts is called, the rest of the following prompts also execute, even when not in the same method.

class Query {
    constructor () {
        this.options = ['View All Departments', 'View All Roles', 'View All Employees', 'Add Department', 'Add Role', 'Add Employee', 'Update Employee Role']
    };

    optionsMenu() {
        inquirer.prompt({
            type: 'list',
            message: 'What would you like to do next?',
            choices: this.options,
            name: 'userOption'
        })
        .then((res) => {
            switch(res.userOption) {
                case 'View All Departments':
                    this.viewDept();
                case 'View All Roles':
                    this.viewRoles();
                case 'View All Employees':
                    this.viewEmp();
                case 'Add Department':
                    this.addDept();
                case 'Add Role':
                    this.addRole();
                case 'Add Employee':
                    this.addEmp();
                case 'Update Employee Role':
                    this.updateEmp();
            }
        })
    }
    
    viewDept() {}

    viewRoles() {}

    viewEmp() {}

    async addDept() {
        const inquiry = await inquirer.prompt({
            type: 'input',
            message: 'What is the name of the new department?',
            name: 'department'
        })

        const newDept = new Department(inquiry.department);
        newDept.insertDept().then(() => {
            this.optionsMenu();
        });
    }

    async addRole() {
        //Query DB for available departments to tie to the role being added
        const departments = await mysql.db.promise().query('SELECT * FROM department').then((results) => {return results[0]});

        //Map departments into an array for the inquirer question
        const departmentChoice = departments.map(x => x.name)

        //Query DB for roles
        const roleQuery = await mysql.db.promise().query('SELECT * FROM role').then((results) => {return results[0]});

        //Map roles into an array for the inquirer validation
        const roles = roleQuery.map(x => x.title.toLowerCase());
        
        //Inquirer prompts for role information from User
        const inquiry = await inquirer.prompt([
            {
                type: 'input',
                message: 'What is the name of the role you would like to add?',
                name: 'role',
                validate: (input) => {
                    //Check input against existing roles to make sure it is unique
                    let lowercase = input.toLowerCase
                    if (roles.includes(lowercase)) {
                        return 'Role already exists'
                    } else {
                        return true;
                    }
                }
            },
            {
                type: 'list',
                message: 'Which department does the role belong to?',
                choices: departmentChoice,
                name: 'department'
            },
            {
                type: 'number',
                message: 'What is the salary of the new role?',
                name: 'salary'
            }
        ])

        //Loop through deparments array to find matching department id
        for(const i = 0; i < departments.length; i++) {
            if (Object.values(departments[i]).includes(inquiry.department)) {
                const newRole = new Role(inquiry.role, departments[i].id, inquiry.salary)
                newRole.insertRole().then(() => {this.optionsMenu()})
            }
        }
    }

    async addEmp() {
        //Query DB for roles
        const roleQuery = await mysql.db.promise().query('SELECT * FROM role').then((results) => {return results[0]});

        //Map roles into an array for the inquirer question
        const roleChoice = roleQuery.map(x => x.title);

        //Query DB for employees
        const employeeQuery = await mysql.db.promise().query('SELECT * FROM employee').then((results) => {return results[0]});

        //Map employees into an array for the inquirer question
        const employeeChoice = employeeQuery.map(x => x.first_name + " " + x.last_name);

        //Add 'None' as an option for employees
        employeeChoice.push('None');

        const inquiry = await inquirer.prompt([
            {
                type: 'input',
                message: "What is the employee's first name?",
                name: 'firstName',
                validate: (input) => {
                    if (!input) {
                        return 'First name cannot be blank.'
                    } else {
                        return true;
                    }
                }
            },
            {
                type: 'input',
                message: "What is the employee's last name?",
                name: 'lastName',
                validate: (input) => {
                    if (!input) {
                        return 'Last name cannot be blank.'
                    } else {
                        return true;
                    }
                }
            },
            {
                type: 'list',
                message: "Which role will the employee take?",
                choices: roleChoice,
                name: 'role'
            },
            {
                type: 'list',
                message: "Enter the employee's manager:",
                choices: employeeChoice,
                name: 'manager'
            }
        ])
    }

    updateEmp() {}
}

When setting up switch statement, you must put a break after every case.

What happens if I forgot a break?

If you forget a break then the script will run from the case where the criterion is met and will run the cases after that regardless if a criterion was met.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM