Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

407
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!