简体   繁体   中英

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. 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. 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.

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 should ease the process of

  • 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.

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. Here's a simple node cli that does what you're after, I think. Just save this code in a file named index.js and go to the directory and type: 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM