首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用node inquirer包返回值

使用node inquirer包返回值
EN

Stack Overflow用户
提问于 2017-07-16 23:04:56
回答 2查看 893关注 0票数 0

我正在尝试使用node.js查询器包来运行一个简单的抽认卡生成器。我在获取返回用户单击的复选框的语法时遇到了问题。因此,一旦用户做出选择,我希望能够记录该选择的结果。目前这个console.log()返回"undefined“。

感谢任何人的帮助!

代码语言:javascript
复制
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])
});
EN

回答 2

Stack Overflow用户

发布于 2017-12-11 03:59:05

代码语言:javascript
复制
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应该只是一个字符串数组。然后,将返回一个包含所选项目的数组,例如:

代码语言:javascript
复制
[ 'Create a Cloze Card', 'Run the flashcards!' ]

希望这能有所帮助!

票数 0
EN

Stack Overflow用户

发布于 2021-06-26 13:16:19

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45130039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档