Search code examples
javascriptnode.jsuser-inputline-by-line

Line-By-Line Node JS prompting for Input not functioning


I have started working with node js and am working with the line-by-line dependency. I have properly installed line-by-line and it is within the folder. Currently, my application will run but will not prompt the user to input an answer. My code is as follows:

var rlname;
var rlage;
var rlcolor;

// Create reading interface
const readline = require('readline');

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

// Ask a question
function getUserInput()
{
    rl.question("What is your name? \n", (name) => {
        console.log(name);
        rlname = name;
        // Log answer in JSON
    })

    rl.question("What is your age? \n", (age) => {
        console.log(age);
        rlage = age;
        // Log answer in JSON
    })

    rl.question("What is your favorite color? \n", (color) => {
        console.log(color);
        rlcolor = color;
        // Log answer in JSON
    })

    console.log("Hello " + rlname + ", you are " + rlage + " years old and your favorite color is " + rlcolor + ".");
}

getUserInput();
rl.close();

This is the produced result:

What is your name?
Hello undefined, you are undefined years old and your favorite color is undefined.

Solution

  • The problem here is caused by the fact that this code should be asynchronous, but it is written as if it was synchronous.

    To be more clear: you ask for the name, but the code is not waiting for the input, as it is asynchronous. It continues down to the console.log statement and proceed to close the input with rl.close().

    You should put each question inside the callback of the previous one, and the closing statement in the last callback:

    var rlname;
    var rlage;
    var rlcolor;
    
    // Create reading interface
    const readline = require('readline');
    
    const rl = readline.createInterface ({
        input: process.stdin,
        output: process.stdout,
        prompt: '>'
    });
    
    // Ask a question
    function getUserInput()
    {
        // FIRST QUESTION
        rl.question("What is your name? \n", (name) => {
            console.log(name);
            rlname = name;
            
            // SECOND QUESTION
            rl.question("What is your age? \n", (age) => {
                console.log(age);
                rlage = age;
                
                // THIRD QUESTION
                rl.question("What is your favorite color? \n", (color) => {
                    console.log(color);
                    rlcolor = color;
    
                    // LOG DATA
                    console.log("Hello " + rlname + ", you are " + rlage + " years old and your favorite color is " + rlcolor + ".");
                    
                    // CLOSE CONNECTION
                    rl.close();
                })
            })
        })
    }
    
    getUserInput();