Javascript NodeJS“;readline";模块未输出提示
使用NodeJS,我试图制作一个“note”管理器只是为了好玩,但当我试图使用readline.question()获取用户对他们想做什么的输入(即创建新便笺、删除便笺)时,不会显示提示。关于如何解决这个问题,有什么建议吗 `Javascript NodeJS“;readline";模块未输出提示,javascript,node.js,asynchronous,async-await,readline,Javascript,Node.js,Asynchronous,Async Await,Readline,使用NodeJS,我试图制作一个“note”管理器只是为了好玩,但当我试图使用readline.question()获取用户对他们想做什么的输入(即创建新便笺、删除便笺)时,不会显示提示。关于如何解决这个问题,有什么建议吗 ` `因为您只使用在线编辑器。(至少我正在努力解决您的提示问题。) 我建议 将代码复制到app.js文件中 您将拥有终端选项卡。请先安装依赖项 npm安装-g异步 npm安装-g readline 通过它,您将在树下拥有node\u modules文件夹 然后点击node
`因为您只使用在线编辑器。(至少我正在努力解决您的提示问题。) 我建议 将代码复制到
app.js
文件中
您将拥有终端
选项卡。请先安装依赖项
npm安装-g异步
npm安装-g readline
通过它,您将在树下拥有node\u modules
文件夹
然后点击node app.js
链接左侧以黑色突出显示
关于代码,您应该注意以下几点:
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);
}
);
reply
分配一些默认值,也许您可以按照var reply=0
reply=0
,则将列表代码包装为条件if(reply==“1”)
此条件将严格检查字符串。如果(回复==1)使用
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);
}
);
供您参考:
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);
}
);
您是否能够获得
的列表,请按一个数字:1:创建一个新文件。2:查看文件。3:添加到文件。4:删除一个文件。5:退出此程序。
@HardikShah当我运行程序时,列表不会输出到控制台,如果这是您的意思的话。在我的情况下,列表非常完美,但在选择1作为选项后,什么都没有发生。你能添加NodeJS异步和readline模块版本吗?@HardikShah在我的在线ide(repl.it)上,异步模块是2.6.1版本,但是我认为实际上不支持readline模块,这解释了为什么我没有输出选项列表是readline
模块不支持repl.it在线ide。你能试试你当地的环境吗?你当地的环境行为是什么?