const employeeQuestion = [
{
type: "list",
name: "employeeTitle",
message: "What's the employee's title",
choices: ["Engineer", "Intern"]
},
//when role is engineer is true, ask this question
{
when: input => {
return input.role == "Engineer"
},
type: "input",
name: "github",
message: "Enter your github username:",
},
//when role is intern is true, ask this question
{
when: input => {
return input.role == "Intern"
},
type: "input",
name: "school",
message: "What's the school you enrolled in ?",
},
]
我的想法是我想利用查询者的何时方法,因此询问用户的github还是学校的问题取决于雇员头衔的答案。但是,当我在命令行上运行node时,问github / school的问题从未出现。我想知道我是否使用了错误的方法,或者是否还有其他选择。
答案 0 :(得分:1)
查询者的 when 方法绝对适合这种情况!
有两个可能的原因:
您正在使用input.role
,但从未在答案哈希中定义role
值。您需要参考上一个问题中的 name 值,在本例中为input.employeeTitle
。
如果原因1无法解决问题,请尝试扩展功能。 何时需要将答案哈希作为输入,然后对其应用条件,并显式返回布尔值。例如。
{
type: "input",
name: "github",
message: "Enter your github username:",
when: (answers) => {
if (answers.employeeTitle === "Engineer") {
return true;
}
}