User Login to Node.js

I am writing a program that will create an array of numbers and double the contents of each array and save the result as a key / value pair. I used to hardcode the array, so everything was fine.

Now I changed the logic a bit, I want to take data from users, and then save the value in an array.

My problem is that I cannot figure out how to do this using node.js. I installed the invitation module using npm install prompt and also looked at the documentation, but nothing works.

I know that I am making a small mistake here.

Here is my code:

//Javascript program to read the content of array of numbers //Double each element //Storing the value in an object as key/value pair. //var Num=[2,10,30,50,100]; //Array initialization var Num = new Array(); var i; var obj = {}; //Object initialization function my_arr(N) { return N;} //Reads the contents of array function doubling(N_doubled) //Doubles the content of array { doubled_number = my_arr(N_doubled); return doubled_number * 2; } //outside function call var prompt = require('prompt'); prompt.start(); while(i!== "QUIT") { i = require('prompt'); Num.push(i); } console.log(Num); for(var i=0; i< Num.length; i++) { var original_value = my_arr(Num[i]); //storing the original values of array var doubled_value = doubling(Num[i]); //storing the content multiplied by two obj[original_value] = doubled_value; //object mapping } console.log(obj); //printing the final result as key/value pair 

Please help me with this, thanks.

+14
javascript
source share
4 answers

The request is asynchronous, so you should use it asynchronously.

 var prompt = require('prompt') , arr = []; function getAnother() { prompt.get('number', function(err, result) { if (err) done(); else { arr.push(parseInt(result.number, 10)); getAnother(); } }) } function done() { console.log(arr); } prompt.start(); getAnother(); 

This will push the numbers before arr until you press Ctrl + C , after which done will be called.

+7
source share

For those who do not want to import another module, you can use the standard nodejs process.

 function prompt(question, callback) { var stdin = process.stdin, stdout = process.stdout; stdin.resume(); stdout.write(question); stdin.once('data', function (data) { callback(data.toString().trim()); }); } 

Use case

 prompt('Whats your name?', function (input) { console.log(input); process.exit(); }); 
+35
source share

A modern example of Node.js with ES6 promises and without third-party libraries.

Rick provided an excellent starting point, but here is a more complete example of how one prompts question by question and will be able to refer to these answers later. Since reading / writing is asynchronous, promises / callbacks are the only way to code such a stream in JavaScript.

 const { stdin, stdout } = process; function prompt(question) { return new Promise((resolve, reject) => { stdin.resume(); stdout.write(question); stdin.on('data', data => resolve(data.toString().trim())); stdin.on('error', err => reject(err)); }); } async function main() { try { const name = await prompt("What your name? ") const age = await prompt("What your age? "); const email = await prompt("What your email address? "); const user = { name, age, email }; console.log(user); stdin.pause(); } catch(error) { console.log("There an error!"); console.log(error); } process.exit(); } main(); 
+4
source share

Node.js implemented a simple readline module that does this asynchronously:

https://nodejs.org/api/readline.html

 const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('What do you think of Node.js? ', (answer) => { // TODO: Log the answer in a database console.log('Thank you for your valuable feedback: ${answer}'); rl.close(); }); 
0
source share