Node.js readline:意外的令牌=> [英] Node.js readline: Unexpected token =>

查看:120
本文介绍了Node.js readline:意外的令牌=>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习node.js并且需要使用 readline 来创建项目。我直接从 readline模块示例中获得以下代码。

I am learning node.js and need to use readline for a project. I have the following code directly from the readline module example.

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('What do you think of Node.js? ', (answer) => {
  // TODO: Log the answer in a database
  console.log('Thank you for your valuable feedback:', answer);

  rl.close();
});

但是当我通过
节点运行代码时try.js 命令,它不断发出如下错误:

But when I run the code via node try.js command, it keeps giving out the errors like below:

rl.question('What is your favorite food?', (answer) => {
                                                    ^^
SyntaxError: Unexpected token =>
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3


推荐答案

箭头功能 ECMAScript 6标准,仅在版本4.0.0

您可以升级node.js版本或使用旧语法,如下所示: / p>

You can either upgrade your node.js version or use the old syntax, which would look like this:

rl.question('What do you think of Node.js? ', function(answer) {
  // TODO: Log the answer in a database
  console.log('Thank you for your valuable feedback:', answer);

  rl.close();
});

(请注意,这些语法之间还有一个区别: this 变量的行为方式不同。这个例子无关紧要,但在其他例子中也可能。)

(Note that there is one more difference between those syntaxes: The this variable behaves differently. It doesn't matter for this example, but it may in others.)

这篇关于Node.js readline:意外的令牌=>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆