首页 > 解决方案 > Nodejs:npm 查询器不工作的类型输入问题

问题描述

我是npm inquirer第一次使用。

我正在使用与此类似的代码:

const inquirer = require("inquirer");

const questions = [
  {
    type: "checkbox",
    name: "collections.telemetria",
    message: "Select collections of database telemetria",
    choices: [
      "chimera-11/14/2019,-4:22:38-PM",
      "chimera-11/14/2019,-4:28:26-PM"
    ]
  },
  {
    type: "checkbox",
    name: "collections.testa",
    message: "Select collections of database testa",
    choices: ["testa_c"]
  }
];

async function main() {
  const collections = (await inquirer.prompt(questions)).collections;
  console.log("collections:", collections);
  const outPath = await inquirer.prompt([
    {
      type: "input",
      name: "outPath",
      default: "./",
      message: "Insert the output path"
    }
  ]).outPath;
  console.log(outPath);
}
main();

问题是,当涉及到要回答的类型输入的问题时,出现了 undefined 这个词,我无法输入任何内容。

在此处输入图像描述

这是一个代码沙箱:https ://codesandbox.io/s/stoic-kowalevski-dgg5u

标签: node.jsinquirerinquirerjs

解决方案


感谢Shivam Sood的建议,我发现代码中只有一个错误。在调用预期结果的属性之前,我忘了把await inquirer.prompt([...])括号放在里面。outPath

所以正确的代码应该是:


const outPath = (await inquirer.prompt([
    {
      type: "input",
      name: "outPath",
      default: "./",
      message: "Insert the output path"
    }
  ])).outPath;
  console.log(outPath);


推荐阅读