3

我正在使用 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 代码看起来像这样

执行查询.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选项中选择Exit并按 Y选项时,我从Exit.js文件中收到此错误

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

3 回答 3

1

这是一个循环依赖的场景。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')

于 2021-12-24T18:08:30.607 回答
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;
于 2021-12-24T17:25:26.310 回答
0

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

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

生成器函数* 探索 ES6 © 2015 - 2018 Axel Rauschmayer (封面由 Fran Caye)

RXJS 指南 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-12-24T19:16:01.420 回答