I am trying to fetch form data, query database and find number of documents that are related to that data, and console that count (for simplicity).
app.post('/process_get', function(req, res){
response = {
first_name : req.body.first_name,
last_name : req.body.last_name,
gender: req.body.gender
};
dbConn.then( function (db) {
var dbo = db.db("azima");
var count = dbo.collection('users').find({first_name: req.body.first_name}, {gender: req.body.gender}).count();
console.log(count);
});
console.log(response);
//convert the response in JSON format
res.end('Data received:\n' + JSON.stringify(req.body));
});
But I'm getting following:
Example app listening at http://:::8888
{ first_name: 'Najar Man', last_name: 'Malakar', gender: 'Male' }
Promise { <pending> }
I know this is because of asynchronous nature, but I don't know how to perform this "simple" operation, since I am new to node.js and mongodb.
How do I store the count value to variable properly?
dbConn, await it, or use.thenon it...