node.js/express/mongodb wait for query results
-
Facing the following problem: there is a simple web application (node.js/express/mongodb). user enters product id and the application shows price.
function queryDb(var id) {
// connect mongodb
// find product price for id
return price
}app.post("/", function(req,res) {
var id = req.body.id;
var price = queryDb(id);
// ---> wait here for the query to finish
res.render('index', {price: price})
})the app runs faster then the db query. how can I force the code to wait for queryDb() function to finish and return the price? I have tried callbacks, promises, async/await - always the same.
-
Just FYI this isn't a general forum for Node.js stuff, it's about the NodeBB forum software.
Anyways, you'll need to share much more code than what you have there, as anything involving the database is unlikely to be synchronous, so you can't just return from the function like that.