Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

187
Visualizações
MySQL2 - How to get only the result & not to include fields using ASYNC/AWAIT

How to get only the result from the connection query? Right now I'm using this approach. Is there a better way to do it?

  const [author] = await connection.query(`SELECT * FROM authors where id=${book.authorId} LIMIT 1`) 

  return author.shift()

OR

    const [author] = await connection.query(`SELECT * FROM authors where id=${book.authorId} LIMIT 1`)
    return author[0]

If I use this approach

const result = await connection.query(`SELECT * FROM authors where id=${book.authorId} LIMIT 1`) 

It returns

 [ { id: 2, name: 'J. R. R. Tolkien' } ],
  [
    ColumnDefinition {
      _buf: <Buffer 01 00 00 01 02 33 00 00 02 03 64 65 66 0b 77 64 73 2d 67 72 61 70 68 71 6c 07 61 75 74 68 6f 72 73 07 61 75 74 68 6f 72 73 02 69 64 02 69 64 0c 3f 00 ... 114 more bytes>,
      _clientEncoding: 'utf8',
      _catalogLength: 3,
      _catalogStart: 10,
      _schemaLength: 11,
      _schemaStart: 14,
      _tableLength: 7,
      _tableStart: 26,
      _orgTableLength: 7,
      _orgTableStart: 34,
      _orgNameLength: 2,
      _orgNameStart: 45,
      characterSet: 63,
      encoding: 'binary',
      name: 'id',
      columnLength: 11,
      columnType: 3,
      flags: 16899,
      decimals: 0
    },

I don't need the fields data(the ColumnDefinition) from the connection.query

What I want is this

{ id: 2, name: 'J. R. R. Tolkien' }

without the solid bracket []

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I finally found a different approach, this time I'm using nested array destructuring

const [[author]] = await connection.query(`SELECT * FROM authors where id=${book.authorId} LIMIT 1`)
return author

You can read this blog to learn more about ES6 - Destructuring

ES6 - Understanding Destructuring

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda