对询问者的下一个问题使用以前的答案

Using previous answer for the next question in inquirer

我想在询问者的下一个问题中使用上一个答案,这是我的代码:

const promptForMissingOptions = async (options) => {
  const questions = [];

  if (!options.name) {
    questions.push({
      type: "input",
      name: "name",
      message: "Name:",
    });
  }

  if (!options.file) {
    questions.push({
      type: "checkbox",
      name: "lots",
      message: "Select the categories:",
      pageSize: 30,
      choices: () => {
        const files = helpers.getFileNames();
        return Object.keys(files);
      },
    });
  }

  if (answers.lots) {
    questions.push({
      type: "checkbox",
      name: "files",
      message: "Select the files:",
      pageSize: 50,
      choices: () => {
        const choices = helpers.getFileNames();
        return Object.keys(
          Object.keys(x)
            .filter((key) => answers.lots.includes(key))
            .reduce((obj, key) => {
              obj[key] = x[key];
              return obj;
            }, {})
        ).reduce(
          (r, k) =>
            r.concat(
              new inquirer.Separator(
                chalk.underline.bold.bgMagenta(
                  "------------------------ " + k + " ------------------------"
                )
              ),
              choices[k]
            ),
          []
        );
      },
    });
  }

  const answers = await inquirer.prompt(questions);

  return {
    files: options.file ? [options.file] : answers.files,
    name: options.name || answers.name,
  };
};


let options = await promptForMissingOptions(options);

这里我想做的是用第二个问题的答案来做第三个问题

我在这里尝试了解决方案:Is there a way to use previous answers in inquirer when presenting a prompt (inquirer v6)?

这对我的情况不起作用。

我该如何解决这个问题?

提前致谢。

在我回答你的问题之前,我想指出一些对你的示例代码的优化,这将有助于解决这个问题:

您目前在哪里使用它:

const questions = [];

if (!options.name) {
  questions.push({ question });
}

您可以通过以下方式实现同​​样的效果:

const questions = [{
  type: "input",
  name: "name",
  message: "Name:",
  when: () => options.name,
}];

来自 the docs 的两点:

when: (Function, Boolean) Receive the current user answers hash and should return true or false depending on whether or not this question should be asked. The value can also be a simple boolean.

这里的措辞有点误导,但归结起来就是当你分配给 when 的函数被调用时,它会得到 当前用户的答案 作为第一个参数传递给它,最终你的函数必须 return truefalse 来确定它是否 应该显示 用户。

choices: (Array|Function) Choices array or a function returning a choices array. If defined as a function, the first parameter will be the current inquirer session answers. [...]

描述确实继续讨论可能的答案值类型,但这里的关键要点是第一个函数参数:“当前询问者会话答案”。在问题 3 处,您可以在 .

中访问问题 2 的答案

Also, just to throw in a little "personal opinion" here, I would highly recommend you move the logic that depends on Object.keys() out of your question choices handlers.. Rather pass in the resulting array of names to the question when you call it (i.e.: your current options argument). If any aspect of the structure changes, it will most likely do so somewhere else; if you keep this logic here, then all changes to that logic will require updates in both places in your code.


现在,为了将此概念应用于您的 示例问题数组,为简洁起见,我将使用一个应用上述概念的示例。

const promptForMissingOptions = async (options) => {
  const questions = [
    {
      type: "input",
      name: "name",
      message: "Name:",
      when: () => options.name,
    },
    {
      type: "checkbox",
      name: "lots",
      message: "Select the categories:",
      when: () => options.file,
      pageSize: 30,
      choices: () => options.categoryChoices,
    },
    {
      type: "checkbox",
      name: "files",
      message: "Select the files:",
      when: () => answers.lots.length,
      pageSize: 50,
      choices: (answers) => {
        const filteredList = [];
        const selectedCategories = answers.lots; // where the ID is the relevant question's "name".
        // now do your "filter" handling here..
        return filteredList;
      },
    },
  ];

  const answers = await inquirer.prompt(questions);

  return {
    files: options.file ? [options.file] : answers.files,
    name: options.name || answers.name,
  };
};

let options = await promptForMissingOptions(options);

我相信代码本身和问题 3 中的小评论在这里应该足够清楚了,但是请告诉我这是否有帮助?