NodeJS "readline" 模块没有输出提示

NodeJS "readline" module not outputting prompt

使用 NodeJS,我试图制作一个 'note' 管理器只是为了好玩,但是当我尝试使用 readline.question() 来获取用户对他们想做什么的输入时(即创建新笔记,删除笔记),不会显示提示。关于如何解决这个问题有什么建议吗?

Link To Project

`

    fileDatabase = [];
    var reply;
    var FileName;
    var FileContent;

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

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

    class NewFile {
        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){ 
            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.", function(answer) {
                var reply = answer
                console.log(reply)
                rl.close();
            });

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

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

`

因为您只使用在线编辑器。 (至少我在努力解决你提示issue的问题。)

我建议 https://www.katacoda.com/courses/nodejs/playground

将您的代码复制到 app.js 文件中。

您将看到 Terminal 选项卡。请先安装依赖。

npm install -g asynch

npm install -g readline

这样,您将在树下有 node_modules 个文件夹。

然后单击 node app.js link 左侧以黑色突出显示。

你应该注意你的代码的几件事:

  1. 请尝试分配一些默认值 reply 也许你可以这样做 var reply = 0
  2. 将列表代码包装到条件if reply = 0
  3. if (reply === "1") 这个条件会严格检查字符串。使用 if(reply == 1).
  4. 并根据您的要求修改代码以进入下一题。

修改代码如下:

fileDatabase = [];
var reply = 0;
var FileName;
var FileContent;

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

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

class NewFile {
    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) {
            var FileName = answer;
            rl.close();
        });
        rl.question(
            'Write onto your file. You will be able to edit it later.\n',
            function(answer) {
                var FileContent = answer;
                rl.close();
            }
        );
    }
    setTimeout(callback, 1000);
},

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

供您参考: