简体   繁体   English

接受用户的多个输入

[英]Take multiple inputs from user

I want to get user input from user. 我想从用户那里获得用户输入。 I have created a function to do that by taking the code from node.js documentation. 我已经创建了一个函数,通过从node.js文档中获取代码来做到这一点。 But I cannot receive multiple user inputs. 但是我无法收到多个用户输入。 How can I do that? 我怎样才能做到这一点? This is the code so far. 到目前为止,这是代码。

function getUserInput() {
        rl.question('Please input a letter: ', (answer) => {
        console.log('Letter entered: ${answer}');
        rl.close();
        }); 
    }

//getUserInput();

var k=0; 
while ( k < 3 ){
        getUserInput();
        k++;
    } 

I expect to take for example 3 user inputs. 我希望以3个用户输入为例。 I want to take user input. 我想接受用户输入。 With the code above I can only take only ONE user Input. 使用上面的代码,我只能接受一个用户输入。 I thought adding the function into a loop so it could work. 我以为将函数添加到循环中可以正常工作。 I am looking for any modification into my code so It can work and so I can take more than one user input. 我正在寻找对我的代码进行的任何修改,以便它可以正常工作,因此我可以接受多个用户输入。

Inquirer is probably what you're looking for, assuming that you're trying to get user input on the command line. 假设您试图在命令行上获取用户输入,那么Inquirer可能就是您要查找的内容。

Description taken from the repo itself: 描述来自仓库本身:

Inquirer.js strives to be an easily embeddable and beautiful command line interface for Node.js (and perhaps the "CLI Xanadu"). Inquirer.js致力于成为Node.js(也许还有“ CLI Xanadu”)的易于嵌入且美观的命令行界面。

Inquirer.js should ease the process of Inquirer.js应该简化

  • providing error feedback 提供错误反馈
  • asking questions 问问题
  • parsing input 解析输入
  • validating answers 验证答案
  • managing hierarchical prompts 管理分层提示

Also, here's an example on how to use it: 另外,这是一个有关如何使用它的示例:

const inquirer = require('inquirer');

const questions = [
  {
    type: 'input',
    name: 'first_name',
    message: "What's your first name"
  }, {
    type: 'input',
    name: 'last_name',
    message: "What's your last name",
    default: function() {
      return 'Doe';
    }
  }
];

inquirer.prompt(questions).then(answers => {
  console.log(JSON.stringify(answers, null, '  '));
});

Cheers. 干杯。

This appears to be using something like the promises API. 这似乎正在使用诸如promises API之类的东西。

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
}); 


function getUserInput(n) {
    rl.question('Please input a letter: ', (answer) => {
        console.log(`Letter entered: ${answer}`);
        if (n < 3) {
            getUserInput(n+1);
        } else {
            rl.close();
        }
    }); 
}

getUserInput(1);

When I run it, this happens: 当我运行它时,会发生以下情况:

$ node getinput.js
Please input a letter: a
Letter entered: a
Please input a letter: b
Letter entered: b
Please input a letter: c
Letter entered: c

I think the problem is that rl.question waits for an input before proceeding but the while loop doesn't. 我认为问题在于rl.question在继续进行之前等待输入,而while循环则没有。 Here's a simple node cli that does what you're after, I think. 我认为这是一个简单的节点cli,它可以满足您的需求。 Just save this code in a file named index.js and go to the directory and type: node index.js 只需将此代码保存在名为index.js的文件中,然后转到目录并键入: node index.js

 // Dependencies
 var readline = require('readline');

 var cli = {};

 // Init function
 cli.init = function(){
   // Send the start message to the console in magenta
   console.log('\x1b[35m%s\x1b[0m',"The CLI is running");

   // Start the interface
   var _interface = readline.createInterface({
     input: process.stdin,
     output : process.stdout,
     prompt : '>'
   });

   var arrayOfInputs = [];
   var k = 0;
   var max = 3;
   cli.getUserInput = function(){
     _interface.question("Ask Something? ", function(str){
       k++;
      arrayOfInputs.push(str);
      if(k < max){
        cli.getUserInput();
      } else {
        console.log(...arrayOfInputs);
      }
    });
   };

   cli.getUserInput();

 };

 cli.init();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 javascript 中的控制台获取多个输入? - How to take multiple inputs from console in javascript? 当用户输入多行文本时,当我从HTML文本区域获取值时,JSON无效 - JSON is invalid when I take the value from HTML textarea when the user inputs text with multiple lines 如何在 Javascript 中从用户那里获取多个输入? 如何为所有人声明变量? - How to take multiple inputs from the user in Javascript? How to declare variable for all? 如何从输入中获取多个字符串并将它们放入同一数组中? - How to take multiple strings from inputs and place them into the same array? 如何在Typeahead中接受多个输入? - How to take multiple inputs in Typeahead? 如何从文本字段中获取用户输入,并在Google脚本中将其设置为变量? - How do I take user inputs from a textfield and make them variables in a Google Script? 保存用户从动态添加的字段中的多个输入 - Saving multiple inputs from dynamically added field by user 我想从多个文本输入中获取输入并将其发送到我的数据库,但是只有最后一个输入发送 - I want to take input from multiple text inputs and send them to my database, but only the last input sends 如何从同一个输入框中获取多个输入并存储它们? - How Do I Take Multiple Inputs From the same Input Box and store them? 使用Nodejs的多个用户输入 - Multiple user inputs using Nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM