Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: findOne returns null but document exists in collection

I'm trying to send an email and password server-side and check if a document with those values exists (which it does), but when I console log the results from the query it's null.

Here's the document in the users collection:

{
    "_id" : ObjectId("580bcf9874ae28934705c0fc"),
    "email" : "[email protected]",
    "password" : "pass"
}

Here's what I'm sending server-side:

{"email":"[email protected]","password":"pass"}

Here's my code (updated):

mongo.connect('mongodb://localhost:27017', function (err, db) {

    if (err) {
    console.log("error: " + err); // logs nothing
    } else {

        var users = db.collection("users");
        var tasks = db.collection("tasks");

        app.post("/login", function(req, res) {

            var emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
            var userInDb;
            var userEmail = req.body.email;
            var userPassword = req.body.password;

            console.log(req.body.email); // logs "[email protected]"
            console.log(req.body.password); // logs "pass"

            if (!userEmail || !userPassword) {
                return res.sendStatus(403);
            } else if ( !emailRegex.test(userEmail)) {
                return res.sendStatus(403);
            } else {

                users.findOne( { "email": userEmail, "password": userPassword }, function(err, results) {

                    console.log(results); // logs "null"

                    if(err) {
                      console.log("error: " + err); // logs nothing
                      res.sendStatus(403);
                    } else {
                      console.log("here"); // logs "here"
                      res.sendStatus(200);
                    }

                  });
              }
          });
       }
    });
like image 924
Spencer K. Avatar asked Nov 01 '16 04:11

Spencer K.


2 Answers

each time you pass a callback that has an error parameter, it's your responsibility to check if an error was passed, and if so, deal with it.

in your code, you have two such callbacks:

mongo.connect('mongodb://localhost:27017', function (err, db)
users.findOne( { "email": userEmail, "password": userPassword }, function(err, results)

either one of them can return an error object that might explain the issue. add the following to the first line of each callback:

if (err) {
  return console.log("error: " + err);
}
like image 123
marmor Avatar answered Nov 16 '22 21:11

marmor


This one worked for me.
I had to call toArray() method. I don't remember how I found that solution, cuz in MongoDB manuals they don't call to array method

users.findOne( { "email": userEmail, "password": userPassword }).toArray()
like image 27
Hamdam Muqimov Avatar answered Nov 16 '22 20:11

Hamdam Muqimov