NodeJS(ES6):SyntaxError:Unexpected token { [英] NodeJS (ES6): SyntaxError: Unexpected token {

查看:2219
本文介绍了NodeJS(ES6):SyntaxError:Unexpected token {的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let {name, description} = req.body;

NodeJS v5.2.0。
尝试使用flags - node bin / www --es_staging --harmony_destructuring。引发一个错误:SyntaxError:意外的令牌{...
我应该使用Babel编译源代码,还是有其他方式在NodeJS中使用ES6?

NodeJS v5.2.0. Tried using flags - node bin/www --es_staging --harmony_destructuring. Throws an error: SyntaxError: Unexpected token {... Should I compile source using Babel, or is there any other way to use ES6 natively in NodeJS?

推荐答案

在Node 5.2.0中,默认情况下不支持解析方法,但如果您使用节点启用它,则会起作用--harmony_destructuring

Destructuring assigments are not supported by default in Node 5.2.0, but it will work if you enable it with node --harmony_destructuring.

但是,现在只有严格模式才支持 let

这是一个V8版本和节点版本5.0.0,其中包括V8版本4.6还将支持 let 以马虎模式。

However let is only supported in strict mode for now.
This is a V8 issue, and Node version 5.0.0, which will include V8 version 4.6 will also support let in sloppy mode.

使用 let 在马虎模式下,将会产生完全相同的错误,所以:

Using let in sloppy mode at the moment, will throw the exact same error you're getting, so :

"use strict"

let {name, description} = {name: "some name", description : "some description"};

在Node 5.2.0中工作正常,请注意对象,在您的情况下 req.body ,必须具有与变量名称相同的键,否则它们将为 undefined

works fine in Node 5.2.0, note that the object, in your case req.body, must have the same keys as the variable names, otherwise they'll be undefined.

这篇关于NodeJS(ES6):SyntaxError:Unexpected token {的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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