简体   繁体   English

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

[英]Variable destructuring assignment after declaration in Javascript

First of all I carefully read the documentation here and checked similar StackOverflow questions.首先,我在这里仔细阅读了文档并检查了类似的 StackOverflow 问题。 I have a sequence of SQL requests.我有一系列 SQL 请求。 When I write this, everything works fine:当我写这篇文章时,一切正常:

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

But I'd like to make several requests, that's why I thought about writing this:但我想提出几个要求,这就是我想写这个的原因:

  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

But I have this error:但我有这个错误:

  { rows } = await db.query(`
         ^
SyntaxError: Unexpected token '='

Seeing the documentation, it appears to be a simple assignment, I don't know what is wrong with this.看文档,好像是一个简单的赋值,不知道这有什么问题。 Any help would be very kind:)任何帮助都会非常友好:)

({rows} = await db.query(...))

you should use parenthesis to signal it's not a block but an assignment你应该用括号来表示它不是一个块,而是一个赋值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM