【问题标题】:how to call another page function name in inquirer node js如何在查询器节点js中调用另一个页面函数名称
【发布时间】:2022-01-20 08:11:56
【问题描述】:

我正在使用 nodejs 中的查询器制作 CLI。

所以在每个选项列表中我都必须给出退出选项,这样如果用户想退出,他/她可以轻松退出。

所以我必须一次又一次地编写 Exit 来避免这个问题,我创建了一个 Exit.js 文件并将 Exit 代码移到那里,这样我就可以一次又一次地使用代码。

退出.js

const executeQuery = require("../executeQuery");

function WantToExit() {
  inquirer
    .prompt([
      {
        name: "moreQuery",
        type: "confirm",
        message: "Want to do anything else?",
      },
    ])
    .then((answer) => {
      if (answer.moreQuery) return executeQuery();
    });
}

module.exports = WantToExit;

我的 executeQuery 代码如下所示

ExecuteQuery.js

const wantToExit = require("../Exit");
const Science = require("../Science");

function executetQuery() {
  inquirer
    .prompt([
      {
        type: "list",
        name: "cmsType",
        message: " Select Subject Options ",
        default: false,
        choices: ["Science", "Maths", "English", "Exit"],
      },
    ])
    .then((answers) => {
      if (answers.cmsType === "Science") {
        Science();
      } else if (answers.cmsType === "Exit") {
        wantToExit();
      }
    });
}

module.exports = executetQuery;

当我从 executeQuery 选项和 press Y 选项中选择 Exit 时,我从 Exit.js 收到此错误> 文件

if (answer.moreQuery) return executeQuery();
                                   ^
TypeError: executeQuery is not a function
at /home/admin/SchoolProject/src/Exit/index.js:13:36

【问题讨论】:

  • 尝试登录executeQuery看看它是什么,因为它不是一个函数

标签: javascript node.js inquirer inquirerjs


【解决方案1】:

您的方法存在问题,因为它产生了模块的循环依赖。您在 ExecuteQuery.js 中有“必需”的 wantToExit,在 Exit.js 中有“必需”的 executetQuery()

我相信你想要实现的是不断询问用户他喜欢的主题,然后根据他/她的选择做一些事情,直到用户选择退出。

我建议在 ExecuteQuery.js 中使用 while 循环作为主提示,并使用布尔标志来检查用户是否要退出。

const wantToExit = require("../Exit");
const Science = require("../Science");

function executetQuery() {

let toStop = false;

// use a while loop
while(!toStop) {
inquirer
    .prompt([
      {
        type: "list",
        name: "cmsType",
        message: " Select Subject Options ",
        default: false,
        choices: ["Science", "Maths", "English", "Exit"],
      },
    ])
    .then(async (answers) => {
      if (answers.cmsType === "Science") {
        // you can also set toStop = true here if you want to 
        // stop after first iteration
        Science();

      } else if (answers.cmsType === "Exit") {
        // wantToExit() now returns a boolean flag
        toStop = await wantToExit();
      }
    });
}
  
}

module.exports = executetQuery;

你的 Exit.js 应该是这样的


function WantToExit() {
  inquirer
    .prompt([
      {
        name: "moreQuery",
        type: "confirm",
        message: "Want to do anything else?",
      },
    ])
    .then((answer) => {
      return !answer.moreQuery;
    });
}

module.exports = WantToExit;

【讨论】:

    【解决方案2】:

    这是一个循环依赖的场景。 A 需要 B,B 需要 A,以此类推。要使其正常工作,您必须修改 module.exports。

    在 Exit.js 文件中,在 ExecuteQuery.js 文件中将 module.exports=WantToExit 更改为 module.exports.WantToExit = WantToExit 并要求它为 const {WantToExit} =require('./Exit.js')。

    类似module.exports.ExecuteQuery=ExecuteQuery 和要求为const {ExecuteQuery} =require('./ExecuteQuery.js')

    【讨论】:

      【解决方案3】:

      我的指导是以某种方式学习 RXJS 和 observables。

      我还认为 (yield* ) 可能在严格模式下工作不确定,我不想这样做,因为这更像是一个可以玩和研究的建议

      Generator Functions* Exploring ES6 © 2015 - 2018 Axel Rauschmayer (cover by Fran Caye)

      RXJS Guide Observable

      const { Observable } = require("rxjs");
      
      async function* wantToExit() {
          (yield* await inquirer
            .prompt([
              {
                name: "moreQuery",
                type: "confirm",
                message: "Want to do anything else?",
              },
            ])
            .then(answer => answer.moreQuery)
          );
        }
      
      const executeQuery = new Observable(subscriber => {
      
          inquirer.prompt([
            {
              type: "list",
              name: "cmsType",
              message: " Select Subject Options ",
              default: false,
              choices: ["Science", "Maths", "English", "Exit"],
            },
          ]).then((answers) => {
            if (answers.cmsType === "Science") {
              subscriber.next(answers.cmsType); 
            } else if (answers.cmsType === "Exit") {
              let doWeExit = await wantToExit().next();
              if (doWeExit === ADD_SOMETHING_NO) {
                  executeQuery.subscribe(userResponse => userResponse);
              } else {
                  console.log('Adios!');
                  return false;
              }
            }
          });
          
      });
      
      module.exports = { executeQuery };
      

      在一个新的页面上,你可以做到。或者你可以直接在函数声明下使用它。希望这对下一步有所帮助。

      const {executeQuery} = require('{INCLUDE YOUR FILEPATH}');
      
      executeQuery.subscribe(userResponse => {
      if(userResponse === 'Science') science();
      
          console.log(userResponse);
      });

      【讨论】:

        猜你喜欢
        • 2021-01-11
        • 2018-04-04
        • 2018-08-28
        • 1970-01-01
        • 2020-07-19
        • 2012-07-25
        • 1970-01-01
        • 2017-06-30
        • 2021-08-21
        相关资源
        最近更新 更多