NodeJS“ readline”模块未输出提示

时间:2018-07-09 17:59:43

标签: javascript node.js asynchronous async-await readline

使用NodeJS,我试图制作一个“笔记”管理器只是为了好玩,但是当我尝试使用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); 
        }
    )

`

1 个答案:

答案 0 :(得分:0)

因为您仅使用在线编辑器。 (至少我正在尝试解决您提示问题的问题。)

我建议I made the layout like this

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

您将拥有Terminal标签。请先安装依赖项。

  

npm install -g异步

     

npm install -g readline

通过这种方式,您将在树下拥有node_modules文件夹。

然后点击左侧用黑色突出显示的node app.js链接。

您应该注意的几对代码:

  1. 请尝试分配一些默认值reply,也许您可​​以作为var reply = 0
  2. 将列表代码包装为条件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);
    }
);

供参考:

https://www.katacoda.com/courses/nodejs/playground