1

我正在尝试获取一些代码,以根据他们对问题给出的答案在屏幕上显示特定消息。这是一个使用 Inquirer 包的节点应用程序,每次我运行节点应用程序时,它都会返回“未定义”。

{
        type: "checkbox",
        name: "channels",
        message: "Which of these TV channels would you watch?!",
        choices: ["Investigation Discovery", "CNN", "Fox News", "TLC"]
    }
]).then(function (responses) {
    for(let i = 0; i < responses.channels; i++) {
        if (responses.channels === 0) {
            console.log("You are probably smart");
        }`enter code here`
        else if (responses.channels === 1) {
            console.log("You are probably well informed");
        }
        else if (responses.channels === 2) {
            console.log("You are probably not very well informed");
        }
        else {
            console.log("You are probably an idiot");
        }
    }

如前所述,它应该根据选择的选项在控制台中返回一条消息,但它只返回“未定义”。

4

2 回答 2

0

这是因为你使用了错误的类型。复选框允许选择多个响应。

对于您的用例,您应该使用listorrawlist和一个对象数组进行选择,以便在显示的内容和之后要使用的内容之间具有不同的值。

所以,类似的事情应该是你所期望的:

const inquirer = require("inquirer");


inquirer
  .prompt([
    {
        type: "list",
        name: "channels",
        message: "Which of these TV channels would you watch?!",
        choices: [{ name: "Investigation Discovery", value: 0 }, { name: "CNN", value: 1 }, { name: "Fox News", value: 2}, { name: "TLC", value: 3}]
    }
  ])
  .then((responses) => {
        if (responses.channels === 0) {
            console.log("You are probably smart");
        }
        else if (responses.channels === 1) {
            console.log("You are probably well informed");
        }
        else if (responses.channels === 2) {
            console.log("You are probably not very well informed");
        }
        else {
            console.log("You are probably an idiot");
        }
});
于 2019-04-10T19:42:04.337 回答
0

好的,所以我设法自己解决了。这是我需要做的:

    if (responses.channels = 1) {
        console.log("You are probably smart");
    }
    else if (responses.channels = 2) {
        console.log("You are probably well informed");
    }
    else if (responses.channels = 3) {
        console.log("You are probably not very well informed");
    }
    else {
        console.log("You are probably an idiot");
    }
于 2019-04-10T20:32:09.753 回答