使用node.js的prompt获取用户输入?

10
我正在用node.js编写一个JS项目,但是我无法正确地让提示框与用户输入交互。我已经从npm安装了它,并按照步骤运行程序,成功弹出提示框,但是我无法将结果存储到变量中。
我的目标是在每个回合时提示用户进行下一步操作(上、下、左或右),并使用该结果。我尝试创建一个全局的移动move变量,并在提示框的部分赋值,但是似乎没有起作用。
  var Moving = function(maxMoves){

    for(var n=maxMoves; n>0;){
        var move;
        var l;
        //This would prompt for a direction (up,down,left,right)
        move = prompt();
        //And this would prompt for the number of tiles to advance;
        l = prompt();
        Direction(move,l);
        n-=l;
    }
};

3
我们无法帮助您处理我们看不到的代码。请将您的问题简化为一个最小可复现的示例(MCVE),并将其放入问题描述中。 - T.J. Crowder
基本上,与我的代码无关,我的问题很简单:如何在node.js中将用户输入存储在变量中? - Dat8StringGuy
1
如何在node.js中从用户获取控制台输入? 的副本。请发布前搜索。有关搜索的更多信息,请单击此处 - T.J. Crowder
4个回答

12

使用require('prompt')require('readline')都是很好的、简单的方法,但我想找到最简单的方法,而且我不关心它是同步还是异步的。找到它花费了我比应该更长的时间,所以如果在深入研究之前阅读了本文,也许我可以帮助节省一些人的时间。

以下是最简单的方法:

# npm install readline-sync

var readline = require('readline-sync');

var name = readline.question("What is your name?");

console.log("Hi " + name + ", nice to meet you.");

免责声明: 我只是在传播消息,以下是我的信息来源:

https://teamtreehouse.com/community/how-to-get-input-in-the-console-in-nodejs

如何从键盘输入文本并将其存储到变量中?


4
当你说“从npm安装它”时,我假设你指的是来自flatiron的prompt模块。
从他们的文档中可以看出,与大多数Node事物一样,它似乎公开了一个异步函数,因此你将在prompt回调中处理输入:
var prompt = require('prompt');

  //
  // Start the prompt
  //
  prompt.start();

  //
  // Get two properties from the user: username and email
  //
  prompt.get(['username', 'email'], function (err, result) {
    //
    // Log the results.
    //
    console.log('Command-line input received:');
    console.log('  username: ' + result.username);
    console.log('  email: ' + result.email);
  });

将它存储在一个变量中与从上面的结果对象访问它没有区别,但要意识到由于它是异步的,它只能在回调函数内部可靠地使用。

我还有点困惑。考虑到它是异步的,我不确定如何将其存储在可以立即使用的变量中。我尝试使用那个模板并添加_move = result;_,但当我尝试_console.log(move)_时,我得到了一个_undefined_。 - Dat8StringGuy
正如我所说,变量只能在提示回调函数中可靠地使用。同样,如果您发布代码,我们可以更轻松地为您提供具体建议的领域。 - Paul
是的,你完全没有按照我的答案来。再说一遍,这是异步的。唯一能得到提示结果的地方就是在回调函数内部。 - Paul

0
你可以使用两个包readlineprompt
这里有一个使用readline包的简单示例https://nodejs.org/en/knowledge/command-line/how-to-prompt-for-command-line-input/
const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('What is your name ? ', function (name) {
  rl.question('Where do you live ? ', function (country) {
    console.log(`${name}, is a citizen of ${country}`);
    rl.close();
  });
});

rl.on('close', function () {
  console.log('\nBYE BYE !!!');
  process.exit(0);
});

这里是另一个使用prompt包的示例。https://www.npmjs.com/package/prompt

const prompt = require('prompt');
prompt.start();
prompt.get(['username','email'], function (err, result) {
    console.log('Command-line input received:');
    console.log('  username: ' + result.username);
    console.log('  email: ' + result.email);
});

-1

inquirer模块具有一个prompt函数。使用它。

const { prompt } = require('inquirer');


prompt([array] | object | string ')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接