如何终止npm查询器提示并将控制返回到主菜单/功能



我一直遇到inquirer npm包的问题,在任何地方都找不到解决方案。我试图允许用户在任何时候退出函数中的查询提示并返回主菜单。然而,这似乎导致查询提示的多个实例保持活动状态,从而导致此错误:

(节点:9756(MaxListenersException警告:检测到可能的EventEmitter内存泄漏。添加了11个出口侦听器。使用发射器.setMaxListeners((增加限制

并且还使得特定功能中的询问者提示开始多次显示同一提示。在尝试使用函数a几次后,我附上了一张行为的图片。提示错误

我已经尝试增加事件侦听器的最大数量,这阻止了内存泄漏错误,但查询器提示错误没有受到影响。

inquirer.prompt([
{
name: 'itemid',
type: 'input',
message: 'Please enter the product id. (type "exit" to return to main menu)',
validate(answer) {
//validates the id provided exists in the database. If exit is entered, returns to main().
var valid = false;
var exit = answer.toLowerCase();
id = parseInt(answer);
if (exit == "exit") {
return main();
}
for (var j = 0; j < idCheckArray.length; j++) {
if (answer == idCheckArray[j]) {
valid = true;
} else {
}
}
if (valid === true && exit != "exit") {
return (true);
} else {
return ("Item ID does not exist. Please enter a valid ID.");
}
}
},

我认为原因是,通过从inquirer-validate函数中返回和调用main,inquirer不会调用其内置的最终函数,因此每次执行此操作时,inquirre创建的事件侦听器都不会被删除。

非常感谢您对如何解决这一问题的任何帮助,谢谢。

我所做的是创建一个名为WantToExit 的函数

const WantToExit = () =>
inquirer
.prompt([
{
name: "moreQuery",
type: "confirm",
message: "Want to do anything else?",
},
])
.then((answer) => {
if (answer.moreQuery) return init();
});

请注意,init是我运行第一个查询提示的主要函数,例如

function init() {
inquirer.prompt(initialQuestionsList).then(async (answer) => {
console.log(answer);
})

最新更新