Saani
Saani

Reputation: 782

Express: Query to check if the document exists

I am developing an application in Express with mongo. I have to check if a particular document exists in the collection. I am doing this:

router.route('/').post(function (req, res, next) {
    var name = req.body.name;
    var dept = req.body.dept;
    var arr = mongoose.model('User').find({'name': name, 'dept': dept});
    if(arr.length() > 0){
        //do something
    }
}

What does mongoose.model('User').find({'pemail': email, 'password': password}); actually return because when I run the app, it gives me this error:

TypeError: undefined is not a function

any help?

Upvotes: 0

Views: 1098

Answers (1)

Abdul Mannan
Abdul Mannan

Reputation: 31

You need to add the call back function, try something like this:

post(function (req, res, next) {
   var name= req.body.name;
   var dept = req.body.dept;
   mongoose.model('User').find({'dept': dept, 'name': name}, function(err, user){
       if(err){
            //do something
       }else{
            //do other thing
       }
   });

}

Upvotes: 1

Related Questions