逐行节点 JS 提示输入不起作用

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

我已经开始使用 node js 并且正在处理逐行依赖项。我已经逐行正确安装,它在文件夹中。目前,我的应用程序将 运行 但不会提示用户输入答案。我的代码如下:

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();

这是生成的结果:

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

这里的问题是 readline.question 是异步的,在您 (a) 可以使用输入和 (b) 创建下一个问题之前,您需要等待 return 的每个回调。但是,您正在 question 调用之后直接执行 console.log 语句。这可能是将 question 包装到承诺中的最简单方法,这样您就可以等待响应。

下面是一个使用 promises 的例子:

与 Node.js 中的许多模块一样,readline 以回调为基础工作。您传递给 rl.question 的回调函数不会立即 运行;只有在收到用户的输入时才会调用它们。所以你的程序试图一次问三个问题,然后在收到任何答案之前立即记录你的“你好”行。 (您可能没有看到第二个或第三个问题,因为 readline 为它们提供了一些内部缓冲区。)

使用回调,正确的做法是这样嵌套:

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;

        rl.question("What is your age? \n", (age) => {
            console.log(age);
            rlage = age;

            rl.question("What is your favorite color? \n", (color) => {
                console.log(color);
                rlcolor = color;

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

                rl.close();
            })
        })
    })
}

getUserInput();

(注意 rl.close 只有在所有问题都被问完后才能被调用,所以在最内层的回调中。)

使用带有 promises 的 async 函数会使这更容易理解,没有嵌套函数和深缩进。

这里的问题是因为这段代码本来应该是异步的,却写成同步的样子

更明确地说:您要求输入名称,但代码不等待输入,因为它是异步的。它继续向下到 console.log 语句并继续使用 rl.close().

关闭输入

你应该把每个问题都放在前一个回调中,最后一个回调中的结束语句:

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();