DEV Community

sm-a
sm-a

Posted on

3

[SOLVED] How I use a synchronous queries with mongoose on NodeJS with ExpressJS

I want to process several querys synchronously one after the other. When I execute the code below, nothing happens. No error message or the same. What am I doing wrong?

router.get('/brand/:smabrand', function(req, res) {
const smaquery = req.query;
// [..]
let getProductCount=async function(smaquery){
let count= await Products.countDocuments(smaquery);
console.log(count)
if (count === 0) {
return res.send("Brand not found!");
}
}
})
view raw test.js hosted with ❤ by GitHub

Top comments (4)

Collapse
 
dmfay profile image
Dian Fay

Unless it's in the code you've omitted, you aren't actually executing getProductCount, only declaring it. I believe you can use async functions as routes with Express now so you shouldn't even need to split things out into a nested function like that.

Collapse
 
sma profile image
sm-a • Edited

Thank you. You have right I do not executing the function. Now it works

Collapse
 
theodesp profile image
Theofanis Despoudis

‘getProductCount’ is a function. You need to call it

Collapse
 
sma profile image
sm-a • Edited

Thank you. You have right I do not executing the function. Now it works.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay