javascriptnode.jsinquirerjs

Using inquirer js inside a loop is not waiting for user input


I have an array of changes to apply to the database and for every change I need user confirmation. Therefore I'm using inquirer js to ask the user if he wants to apply those changes.

I tried outside the loop and works but if I do it inside the loop it don't. It shows the question but the loop keeps going not letting the user to answer yes or no. I'm looking over SO and the internet and I can't fid any example or code like this, is what amb trying even possible?

This is my code:

changes.map(change => {
                      inquirer.prompt([{
                        name: "operate",
                        type: "confirm",
                        message: `Updatade database with ${change.description}?`,
                    }, ])
                    .then((operate) => {
                        if (operate.replace) {
                            green(
                                `Database has been updated`
                            );
                        } else {
                            red(
                                `Database hs not been updated`
                            );
                        }
                    });}

Also tried with async/await but I'm having the same issue.


Solution

  • I found the issue. On the loop I was creating all the prompts with the same name therefore didn't result. After changing that now it works perfect.