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

问题描述

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

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

上下文:

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

推荐答案

根据 node.green ,使用基元进行对象解构即可. JS v6.4.0,并在该版本以下的Node.js版本上抛出Unexpected Token {.

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.

此外,对象剩余/扩展属性仅适用是从Node v8.6.0开箱即用的.它可以在带有--harmony标志的v8.2.1中工作,并在低于此标志的Node.js版本上抛出Unexpected Token ....

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.

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

07-26 03:21