gpt4 book ai didi

javascript - NodeJS "readline"模块不输出提示

转载 作者:行者123 更新时间:2023-11-30 06:24:34 24 4
gpt4 key购买 nike

使用 NodeJS,我只是为了好玩而尝试制作一个“笔记”管理器,但是当我尝试使用 readline.question() 来获取用户关于他们想做什么的输入(即创建一个新笔记, delete a note),则不会显示提示。关于如何解决此问题的任何建议?

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 链接。

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

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

供您引用:

enter image description here

关于javascript - NodeJS "readline"模块不输出提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51251494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com