What will findOne() return when mongo can't find any record for specific query.

2,728 views
Skip to first unread message

Ashutosh Sharma

unread,
May 20, 2018, 9:34:44 AM5/20/18
to mongodb-user

 I am making a signUp service. my function at backend should return an error if email already exists in the database.
Here is my function at the backend to handle my post method before posting I am checking if the user with the same emailId exits or not.
At line 5, I am checking if some user exits with the emailId entered in the form(sent as a request to the function).
The MongoDB documentation says findOne() will return a cursor when it finds a record for the query provided in the argument.
I want to know what it returns when it could not able to find the record for the provided query.

Please provide a way around.

I am adding the code snippet:
Have a look at the code here for a better view: https://codeshare.io/ld

________

router.post('/newuser', (req,res) =>{
    let user = req.body;
    var query = {"email" : user.email};

    let exist = Users.findOne(query);

    if(exist){
      console.log('User exists');
    }
    else{
      console.log("null");
    }

    res.set({
        'Content-Type' : 'application/json',
        'Access-Control-Allow-Origin' : 'http://localhost:4200'
    })

    if(exist){
      res.json({message: "Already Exists"})
      res.send();
    }
    else{
      //create user
      Users.create(user, function(err, succ){
        if(err){
            console.log("The error is:" + err);
            res.status(400);
            res.send(err);
        }
        else{
            console.log("User created is:" + succ);
            res.send(succ);
        }
    });
  }
});

Tiago Brito

unread,
May 20, 2018, 11:34:12 AM5/20/18
to mongodb-user
Hi Ashutosh

This is what I found in the documentation for 3.6:

"Although similar to the find() method, the findOne() method returns a document rather than a cursor."

Above in the documentation, it also says this:

"If no document satisfies the query, the method returns null."


I hope this helps.
Tiago

Ashutosh Sharma

unread,
May 20, 2018, 3:56:17 PM5/20/18
to mongodb-user
Okay, I got it, So according to my code, line 7 will get the document if it found something with the query. And if it doesn't find anything then it would return null.
Hence, according to my code if some email doesn't exist in the database then it should print 'null'.
But it is not doing that. It is just printing 'User exists'. When I am console logging the exist variable on line 7 it always shows some [Object object].
Why is that happening??

Tiago Brito

unread,
May 20, 2018, 8:04:52 PM5/20/18
to mongodb-user
What does the exist variable print when the email does exist? Still an [Object object] ? It should either be a {} enclosed document or null, not an Object.

Have you tried running that find query in the Mongo shell and see what it returns? What does console.log(user.email) show? How are you initializing Users?

Ashutosh Sharma

unread,
May 21, 2018, 4:07:16 AM5/21/18
to mongodb-user
Yes, I have tried mongo Client. Mongo Client is returning 'null' if the email doesn't exist. And document when some record exists for the particular query. But in both cases javascript returns [Object object]. How can I distinguish between them?

Tiago Brito

unread,
May 21, 2018, 6:41:26 AM5/21/18
to mongodb-user
There should be no distinction between them, since mongo shell also reads in JavaScript. The issue might be with your user.email object or with your Users object. What are their contents?
Reply all
Reply to author
Forward
0 new messages