简体   繁体   中英

Variable destructuring assignment after declaration in Javascript

First of all I carefully read the documentation here and checked similar StackOverflow questions. I have a sequence of SQL requests. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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