Node.js, Express.js - unexpected token {

自闭症网瘾萝莉.ら 提交于 2019-12-20 06:29:59

问题


My app crashes every time it reaches this line:

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

can't seem to locate the exact answer..here is the error log

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

context:

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');
      }
  });
});

回答1:


According to node.green, the object destructuring with primitives syntax works after Node.JS v6.4.0, and throws the Unexpected Token { on Node.js versions below that.

Also, the object rest/spread properties only works out of the box from Node v8.6.0. It works in v8.2.1 with the --harmony flag, and throws the Unexpected Token ... on Node.js versions below that.




回答2:


You tried to use destructuring assignment. AFAIK its support by nodejs v.6+ from a box and from 4.2.2 with flag --harmony_destructuring



来源:https://stackoverflow.com/questions/49042673/node-js-express-js-unexpected-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!