const inquirer = require("inquirer")
var questions = [
{
type: "number",
name: "name",
message: "Please the number of players",
validate: function (name) {
var valid = Number.isInteger(name)
return valid || `Please enter a valid whole number`
},
},
]
function promptUser() {
inquirer
.prompt(questions)
.then((answers) => {
console.log(`You entered ${answers["name"]}!`)
})
.catch((error) => console.log(`Please enter a number`))
}
promptUser()
考虑到上面的代码,我注意到在这样的旧视频中,如果你包含 validate 并且它失败了,输入将被清除。然而,在我的情况下,我得到一个不会自动清除的 NaN。假设我启动应用程序并输入“abcdefg”:
? Please the number of players NaN
>> Please enter a valid whole number
如果我输入任何内容,它只会被添加到 NaN 的末尾。Backspace 和 delete 不会删除 NaN,但是如果我多次按下向上箭头按钮直到不再显示以前的输入,它将被删除。难道我做错了什么?