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

408
Visualizações
How to check if Node Js query is empty or not?

I have been trying to check if a result query is empty or not, I have researched for a while now. The code below shows the query that I'm making and the two ways that I checked if it's empty or not.

The problem is only one block is executed and the other is not executed even if it's supposed to be executed.

db.each(`SELECT category FROM categories WHERE category = ?`, [category], (err, res) => {
        
    if (res != {}) {
        console.log("Not Empty")
    }else{
        console.log(" Empty")
    }
}

On the above, if the res returns something it executes the if statement, otherwise the else is not executed.

I have also tried using Objects.keys(res).lenght > 0, but still nothing.

can you please help me understand this better I'm new to node JS.

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

0

Like Barmar said it is an array that you get back so you can use

if (res  && res.length > 0)
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