Node.js,Express.js - 意外令牌{

时间:2018-03-01 04:33:14

标签: node.js express unexpected-token

我的应用每次到达此行时都会崩溃:

const {name, price} = req.query;
        ^

似乎无法找到确切的答案。错误日志

SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:140:18)
    at node.js:1043:3

上下文:

app.get('/products/add' , (req, res) => {
  const {name, price} = req.query;

  const INSERT_PRODUCTS_QUERY = `INSERT INTO products (name, price) VALUES ('${ name }', ${ price })`;
  connection.query(INSERT_PRODUCTS_QUERY, (err,results) => {
      if(err)
      {
        return res.send(err);
      }
      else
      {
        return res.send('succesfully added product');
      }
  });
});

2 个答案:

答案 0 :(得分:5)

根据object destructuring with primitivesobject rest/spread properties语法在Node.JS v6.4.0之后工作,并在下面的Node.js版本上抛出@Html.TextBoxFor(m => m.MyDate, new {@class = "form-control", @id = "txtMyDate", @type = "date", @Value = Model.MyDate.ToString("yyyy-MM-dd")})

此外,{{3}}仅在Node v8.6.0中开箱即用。它在v8.2.1中使用Unexpected Token {标志,并在下面的Node.js版本上抛出--harmony

答案 1 :(得分:2)

您尝试使用AskOption()。 AFAIK由nodejs v.6 +从一个方框支持,4.2.2由国旗destructuring assignment支持