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 ?",
},
]
The idea is I want to utilize inquirer's when method so the question whether to ask about the user's github or school is dependent on the answer on the employee's title. But when i run node on the command line, the question asking for github / school never appeared. I wonder if I used the method wrong or if there are other alternatives.
inquirer's when method is definitely the correct one for this situation!
There are two possible reasons:
You are using input.role
but never defining a role
value in the answers hash. You need to refer to the name value from the earlier question, in this case, input.employeeTitle
.
If reason 1 does not fix it, try expanding your function a bit. when needs to take in the answers hash as an input, then apply the conditional to that and explicitly return a Boolean. Ex.
{
type: "input",
name: "github",
message: "Enter your github username:",
when: (answers) => {
if (answers.employeeTitle === "Engineer") {
return true;
}
}