Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中声明后的变量分解赋值_Javascript_Destructuring - Fatal编程技术网

Javascript中声明后的变量分解赋值

Javascript中声明后的变量分解赋值,javascript,destructuring,Javascript,Destructuring,首先,我仔细阅读了文档并检查了类似的问题。我有一系列SQL请求。当我写这篇文章时,一切都很顺利: const { rows } = await db.query(` SELECT * FROM table WHERE id = ${id}`) 但我想提出几个要求,这就是为什么我想写这篇文章: let rows; { rows } = await db.query(` SELECT * FROM table WHERE id = ${id}`)

首先,我仔细阅读了文档并检查了类似的问题。我有一系列SQL请求。当我写这篇文章时,一切都很顺利:

  const { rows } = await db.query(`
    SELECT *  FROM table
    WHERE id = ${id}`)
但我想提出几个要求,这就是为什么我想写这篇文章:

  let rows;

  { rows } = await db.query(`
    SELECT *  FROM table
    WHERE id = ${id}`)

  // do something with the rows

  { rows } = await db.query(`
    SELECT *  FROM othertable
    WHERE id = ${id}`)

  // do something else
但我有一个错误:

  { rows } = await db.query(`
         ^
SyntaxError: Unexpected token '='
看到文档,这似乎是一个简单的任务,我不知道这有什么错。任何帮助都将非常感谢:)

({rows}=wait db.query(…)


您应该使用括号来表示它不是一个块,而是一个赋值,作为分解
rows=(wait db.query(/*…*/).rows