简体   繁体   中英

Node.js Inquirer : is there a way to prompt for an user-defined size array?

I need to ask an user for his favorites movies (in CLI environment). I want to let him enter how much entries he want, using a loop and a "More ?" question after each entry.

I would like to keep this "declarative" use of inquirer (as much as possible) :

var questions = [{
  name: 'firstName',
  message: 'What is your first name?'
}, {
  name: 'birthdate',
  message: 'What is your birthdate ?',
}, {
  name: 'favoriteMovies',
  message: 'What are your favorite movies ?',
  // What can I do ?
}]


inquirer(questions, function(answers){
 console.log(answers); //an object containing the user response.
});

So the "favoriteMovies" property would be an array populated with the provided entry.

Is it possible ?

You could set a type of response. like this:

var questions = [{
   name: 'firstName',
   message: 'What is your first name?',
   response: 'string'
}, {
   name: 'birthdate',
   message: 'What is your birthdate ?',
   response: 'string'
}, {
   name: 'favoriteMovies',
   message: 'What are your favorite movies ?',
   response: 'array'
}]

And if the response is array you could as as many times as you want. (also I suggest to have validation parameters for each question.)

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