我正在尝试使用node.js查询器包来运行一个简单的抽认卡生成器。我在获取返回用户单击的复选框的语法时遇到了问题。因此,一旦用户做出选择,我希望能够记录该选择的结果。目前这个console.log()返回"undefined“。
感谢任何人的帮助!
inquirer.prompt ([
{
type: "checkbox",
name: "typeOfCard",
message: "Select an action.",
choices: [
{
name: "Create a Basic Card"
},
{
name: "Create a Cloze Card"
},
{
name: "Run the flashcards!"
}
]
}
]).then(function(answers){
console.log(answers.typeOfCard[0])
});发布于 2017-12-11 03:59:05
const inquirer = require('inquirer');
inquirer.prompt ([
{
type: "checkbox",
name: "typeOfCard",
message: "Select an action.",
choices: [
"Create a Basic Card",
"Create a Cloze Card",
"Run the flashcards!"
]
}
]).then(function(answers){
console.log(answers.typeOfCard);
});choices应该只是一个字符串数组。然后,将返回一个包含所选项目的数组,例如:
[ 'Create a Cloze Card', 'Run the flashcards!' ]希望这能有所帮助!
发布于 2021-06-26 13:16:19
const inquirer = require("inquirer");
console.clear();
const main = async() => {
const readCardChoise = () => {
const read = new Promise((resolve, reject) => {
inquirer.prompt ([
{
type: "checkbox",
name: "typeOfCard",
message: "Select an action.",
choices: [
{
name: "Create a Basic Card"
},
{
name: "Create a Cloze Card"
},
{
name: "Run the flashcards!"
}
],
validate(answer) {
if (answer.length < 1) {
return 'You must choose at least one card.';
}
return true;
},
}])
.then((answers) => {
resolve(answers.typeOfCard);
});
});
return read;
}
const cadSelect = await readCardChoise();
console.log(cadSelect)
}
main();https://stackoverflow.com/questions/45130039
复制相似问题