0

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?

1
  • count is probably a promise, so you have to deal with it the same way you do with dbConn, await it, or use .then on it... Commented Sep 21, 2018 at 15:06

1 Answer 1

1

If you are using latest technologies. Use async/await.

   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 (database) {
            var dbo = database.db("azima");
            var count
   dbo.collection('users').find({first_name: req.body.first_name}, 
                                 {gender: req.body.gender})
                                 .count(function(err,resCount){
                                  if(err){
                                    console.log(err)}
                                  else{
                                    count = resCount  
                                    console.log(count);
            res.end('Data received:\n' + JSON.stringify(req.body));
                   }
                 });

    }).catch(function(e){console.log(e)})

    console.log(response);

    //convert the response in JSON format
});

Try to add this if okay then okay else try to share what is in catch block More over when you will send data from mongo query then you will need to put res.send inside the dbConn.then else it will not wait for mongodb query and you will send

Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting Unexpected identifier error pointing to the line await. Also tried async/await
@Razan: can you elaborate to me on a callback function on count()? that'd be great!
@Azima Let me try .count() will give us 2 output error or result. So we take that in function(callback) that we have written and in that function we have check if error is defined(i.e if .count() throw error ) then we we set some action else we perform something else

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.