NodeJS"readline"模块不输出提示 [英] NodeJS "readline" module not outputting prompt
问题描述
使用NodeJS,我试图制作一个笔记"管理器只是为了好玩,但是当我尝试使用readline.question()获得用户对他们想做的事情的输入(即创建一个新的笔记,删除记事),则不会显示提示.关于如何解决此问题有什么建议吗?
Using NodeJS, I was trying to make a 'note' manager just for fun, but when I tried to use readline.question() to get the user's input on what they would like to do(i.e create a new note, delete a note), the prompt wouldn't be displayed. Any suggestions on how I could fix this issue?
`
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);
}
)
`
推荐答案
由于您仅使用在线编辑器. (至少我正在尝试解决您提示问题的问题.)
As you are only using an online editor. (At least I am trying to solve your problem of prompting issue.)
我建议 https://www.katacoda.com/courses/nodejs/playground
将您的代码复制到app.js
文件中.
Copy your code into app.js
file.
您将具有Terminal
选项卡.请先安装依赖项.
You will have the Terminal
tab. Please install dependencies first.
npm install -g异步
npm install -g asynch
npm install -g readline
npm install -g readline
这样,您将在树下拥有node_modules
文件夹.
By which, you will have node_modules
folder under the tree.
然后点击左侧用黑色突出显示的node app.js
链接.
And click on node app.js
link left side highlighted by black color.
夫妇,您应该注意自己的代码:
- 请尝试分配一些默认值
reply
,也许您可以将其设置为var reply = 0
- 将列表代码包装为
reply = 0
的条件. -
if (reply === "1")
此条件将严格检查字符串.改用if(reply == 1)
. - 然后根据需要修改代码,以解决下一个问题.
- Please try to assign some default value of
reply
maybe you can do asvar reply = 0
- Wrap the list code to the condition if
reply = 0
. if (reply === "1")
this condition will strictly check with string. use insteadif(reply == 1)
.- And modify your code as per your requirement to fall into next question.
下面是修改后的代码:
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);
}
);
供参考:
这篇关于NodeJS"readline"模块不输出提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!