Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

409
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda