Andressa Pinheiro
Andressa Pinheiro

Reputation: 1637

How to know if MongoDB query using Mongoose returned a Document

Hi I am doing a query to check if I have a user in my database. I am using Mongoose in Node.JS.

This is my query :

var user;
users.findOne({'fbUID': '1234'}, setFoundUser)

var setFoundUser = function(err, userFound){

if (err) return handleError(err);
user = userFound

}

Well, I am trying to check if the result of the query to see if it returned a document from the Database. This is how I check it:

function getUserFound(){
    return user;

}

var user= getUserFound();

if(user !== undefined ){...
   //do something
}

but, Even when my query returns a document, in other words, the user exists, it never goes inside the if above. For example, if the user exists the 'user' variable will receive the document returned from the DataBase, but I don't know how to check if it is undefined or not.

if user exists it should goes inside the if, but if it does not exist in the database, what is Mongoose returning? false, null, undefined? nothing?

How can I check that the query does not return any document?

Does someone know?

Thank you for your understanding.

Upvotes: 0

Views: 2229

Answers (3)

Badmous
Badmous

Reputation: 135

Although I am using an async function and nestsjs with mongoose.

const sender = this.contactModel.findOne({account:transferDtos.sender});
if(!(await sender)){
   return "invald sender";
}else{
   return sender;
}

so without the async, we can do something like this

if(!sender){
    return "invalid sender";
 }else{
    return sender;
 }

Upvotes: 0

BRogers
BRogers

Reputation: 3624

You're defining user twice in your code btw.

You don't really need a new function to getUser();

I would recommend doing something like this (you may have to toString() the hid, but I'm pretty sure you don't:

var user;

function getUser(uid, callback) {
    users.findOne({'fbUID': uid}, function(err, userFound){

        if (err) return handleError(err);
        callback(userFound);
    });
}

getUser('1234', function(userAccount) {
    if (!userAccount) {
        //handle stuff
        return;
    }

    user = userAccount;
    // check to make sure the user is returned
    console.log(userAccount);
}

Even though the below is recommended for checking for null... you have to make sure the method finishes before checking the variable. In Node.... stuff works a little differently than traditional programming.

For instance:

var record1 = dbCall1(); // takes 5 seconds to return a record
var record2 = [0,1,2]; // instant
if (!record1) {
    // this will always be true because this statement gets executed right away (not after the 5 seconds are up)
}

Node doesn't wait for the dbCall1() to finish before moving on in the code... That's I used a callback in my example. So, in my example... it knows what to do and how to notify you because it notifies (callback) when it's done so that you can continue.

Upvotes: 1

Ben
Ben

Reputation: 5074

The problem is the check to see if user is found is OUTSIDE of the findOne()'s callback. when you do:

if(user !== undefined ){...
   //do something
}

findOne() is not completed yet. Therefore, user is undefined. That's why it never goes inside this if statement. This if statement should be inside findOne()'s callback (which is setFoundUser() in your case). Here is the revised code:

users.findOne({'fbUID': '1234'}, setFoundUser)

var setFoundUser = function(err, userFound){

  if (err) return handleError(err);
  if (!userFound) {
    // inform caller that no user found
  } else {
    // user found, do something with userFound
    console.log(userFound);  // this should print the user object
  }

}

Upvotes: 1

Related Questions