Skip to content Skip to sidebar Skip to footer

Nodejs "readline" Module Not Outputting Prompt

Using NodeJS, I was trying to make a 'note' manager just for fun, but when I tried to use readline.question() to get the user's input on what they would like to do(i.e create a new

Solution 1:

As you are only using an online editor. (At least I am trying to solve your problem of prompting issue.)

I would suggest https://www.katacoda.com/courses/nodejs/playground

Copy your code into app.js file.

You will have the Terminal tab. Please install dependencies first.

npm install -g asynch

npm install -g readline

By which, you will have node_modules folder under the tree.

And click on node app.js link left side highlighted by black color.

Couple of things you should take care about your code:

  1. Please try to assign some default value of reply maybe you can do as var reply = 0
  2. Wrap the list code to the condition if reply = 0.
  3. if (reply === "1") this condition will strictly check with string. use instead if(reply == 1).
  4. And modify your code as per your requirement to fall into next question.

Below is modified code:

fileDatabase = [];
var reply = 0;
varFileName;
varFileContent;

var readline = require('readline');
varasync = require('async');

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

classNewFile {
    constructor(fileName, fileContent) {
        this.fileName = fileName;
        this.fileContent = fileContent;
    }
}

console.log('Hello! Welcome to your file management system.');

async.whilst(
    function() {
        return reply != '5';
    },

    function(callback) {
 
        if (reply === 0) {
            rl.question(
                'Press a number:\n1: Create a new file.\n2: View a file.\n3: Add to a file.\n4: Delete a file.\n5: Exit this program.\n',
                function(answer) {
                     reply = answer;
                    rl.close();
                }
            );
      }

    if (reply == 1) {
        rl.question('What would you like to name this file?\n', function(answer) {
            varFileName = answer;
            rl.close();
        });
        rl.question(
            'Write onto your file. You will be able to edit it later.\n',
            function(answer) {
                varFileContent = answer;
                rl.close();
            }
        );
    }
    setTimeout(callback, 1000);
},

    function(err) {
        console.err('we encountered an error', err);
    }
);

For your reference:

enter image description here

Post a Comment for "Nodejs "readline" Module Not Outputting Prompt"