How can I return the count of documents returned by a query? I have a routing file, which have the following code:
router.post('/facebookLogin', function(req, res, next){
var User=require('../models/user');
var a=User.facebookUserExist(req.body.id, req.body.email);
console.log(a);
res.end();
});
And here is the content of the User model file:
var User=function(data){
this.data=data;
}
User.prototype.data={};
User.prototype.facebookUserExist=function(id, email){
var output;
db.collection('users').find({
$or:[
{
facebookID:id
},
{
email:email
}
]
}).count(function(err, numOfDocs){
output=numOfDocs;
});
return output;
}
module.exports=new User;
I set the value of the output variable in the count method callback, but the function still return undefined.
findandcount, you can use justcountwith the same filter. 2. you are using a callback method as an async method, that's why you get the undefined.