0

I am creating a schema and I am saving a Document in the MongoDB.

Schema:

var userSchema = new Schema({

        userID: Number,
        userName: String,
        userEmail: String,
        teams:Array,
        fbUID: String,
        googleUID: String,
        twitter: String 


});

Document:

var users = mongoose.model('users',userSchema);

    if(socialMediaType == "fbUID"){
         var user = new users({
            userID: id, //give the id of the next user in Dbase
            userName: userName, 
            userEmail: 'userEmail',
            teams:[],
            fbUID : socialMediaID
         });

where socialMediID is a parameter passed in a function.

I am saving:

 user.save(function(err, user){
        if(err) return console.error(err);
        log.d("user saved", user);
     });

My query :

    function searchUser(socialMediaID, socialMediaType){
        var user

        if(socialMediaType == "fbUID"){

             users.findOne({'fbUID': socialMediaID}, function(err, userFound){

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

when I print the user like this:

log.d("user retrieved from Database", user)

it is undefined like this :

user retrieved from Database undefined

When I search on the terminal this:

 db.users.find()

I get this:

{ "_id" : ObjectId("53c6a25ff34dc62bac05f6e9"), "userName" : "Andressa", "userEmail" : "userEmail", "teams" : [ ], "__v" : 0 }
{ "_id" : ObjectId("53c6b3dfedf9710db27740b2"), "userName" : "Andressa", "userEmail" : "userEmail", "fbUID" : "1234", "teams" : [ ], "__v" : 0 }
{ "_id" : ObjectId("53c6b3e0edf9710db27740b4"), "userName" : "Andressa", "userEmail" : "userEmail", "fbUID" : "1234", "teams" : [ ], "__v" : 0 }
{ "_id" : ObjectId("53c6b3e1edf9710db27740b6"), "userName" : "Andressa", "userEmail" : "userEmail", "fbUID" : "1234", "teams" : [ ], "__v" : 0 }
{ "_id" : ObjectId("53c6b3e2edf9710db27740b8"), "userName" : "Andressa", "userEmail" : "userEmail", "fbUID" : "1234", "teams" : [ ], "__v" : 0 }'

When I search this on the terminal:

 db.users.findOne({fbUID:"1234"})

I get this:

    > db.users.findOne({fbUID:"1234"})
{
    "_id" : ObjectId("53c6c8b3dc1ea36cb7f5f8e1"),
    "userName" : "Andressa",
    "userEmail" : "userEmail",
    "fbUID" : "1234",
    "teams" : [ ],
    "__v" : 0
}

Does someone know what I am doing wrong?

Thank you for your understand.

Andressa Pinheiro
  • 1,517
  • 2
  • 18
  • 28
  • After you know the document is saved, could you find the document from the mongodb shell running the corresponding query? – sigurdga Jul 16 '14 at 18:35
  • 1
    Is the example where you do `user = userFound` the real code? In that case it's because you try to return `user` when the value is set in an asynchronous callback. See for example [How to return value from asynchronous function](http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function?rq=1) for how to solve that. – Andreas Hultgren Jul 16 '14 at 18:59
  • @AndreasHultgren, OK. Yes, it's the real code. Thank you. I am gonna check it. – Andressa Pinheiro Jul 16 '14 at 19:05

1 Answers1

2

The problem is that you are coding JavaScript like C.

The find function has a callback, so you can't return a value from it.

When you do this:

users.findOne({'fbUID': socialMediaID}, function(err, userFound){
   if(err) return handleError(err);
   user = userFound; 
});
return user;

The returned value is null because findOne runs async.

To fix simply do this:

function searchUser(socialMediaID, socialMediaType, cb){
    if(socialMediaType == "fbUID") {

        users.findOne({'fbUID': socialMediaID}, cb);
    }
    cb(true);
}

Welcome to callback hell :)

Christian P
  • 12,032
  • 6
  • 60
  • 71
mkoryak
  • 57,086
  • 61
  • 201
  • 257