我正在Linux终端上运行节点时从Node js readline模块运行一个奇怪的行为。该模块确实有效,但它也尝试评估用户输入,就好像它是一个语句一样,返回一个Reference Error。这只是我做的一个简单的测试,但不能让它工作......我已经尝试了很多教程。我的继承人代码:节点js readline继续评估输入并返回错误
'nodeReadlineTeste':function(){
var readline = require('readline');
//without terminal:false I was getting double lettering on terminal input...
var rl = readline.createInterface({input:process.stdin,output:process.stdout,terminal:false});
rl.question('What is your name? ',function(resp){getIt(resp)});
//this function is to bypass NODE assyncronous nature... I was getting the log before even answering.
function getIt (resp){console.log(resp); rl.close()};
},
//运行此方法并回应我的名字,我得到这样的结果:
> global.nodeReadlineTeste()
What is your name? undefined
> Daniel
ReferenceError: Daniel is not defined
at repl:1:1
at ContextifyScript.Script.runInThisContext (vm.js:44:33)
at REPLServer.defaultEval (repl.js:239:29)
at bound (domain.js:301:14)
at REPLServer.runBound [as eval] (domain.js:314:12)
at REPLServer.onLine (repl.js:440:10)
at emitOne (events.js:120:20)
at REPLServer.emit (events.js:210:7)
at REPLServer.Interface._onLine (readline.js:279:10)
at REPLServer.Interface._line (readline.js:626:8)
> Daniel
没有READLINE应该避免正是?也许我做了一些简单的错误,如果是这样,请帮我找到它。先谢谢你。
这不一定是问题,但是当使用节点REPL时,'>'表示它期待执行一个语句/表达式,它当前不会等待用户输入您的问题。因此,尽管这并不能解决问题,但你基本上是告诉节点执行“Daniel”,它不知道该怎么做。 –