0

I am trying to save a new Document (user) in my MongoDb and I use callback. The code runs and goes until save the user, but after that I get an error.So I can save user. I have the following code:

    function saveUser(userName, socialMediaType, socialMediaID, setDocNumber, callback){
        var user;

    if(socialMediaType == "fbUID"){
         user = new users({
            userName: userName, 
            userEmail: 'userEmail',
            teams:[],
            fbUID : socialMediaID
         });
        }else 
          if(socialMediaType =="google"){
      //do the same
}

       var query = {}
        query["'"+ socialMediaType +"'" ] = socialMediaID

         users.findOne(query, function(err, userFound){

           if (err) { // err in query
            log.d("Error in query FoundUser", err)
            log.d("User Found", userFound)
        }else 

        if(userFound == undefined){ //if user does not exist

              user.save(function(err, user){
            if(err) return console.error(err);
            log.d("user saved", user);
            currentSession =  sessionOBJ.login(user._id, socialMediaID);  
            callback(currentSession,"created")

         });

          }else{


            currentSession =  sessionOBJ.login(userFound._id, socialMediaID);  
            callback(currentSession,"logged")

          }


            });

    }

I call the function above through this code:

f(fbUID !== undefined){

        userModelOBJ.saveUser(userName,"fbUID", fbUID, function(currentSession, status) {

            res.send({"status":status,  
                "sessionID": currentSession.sessionID,
                "expires" : currentSession.date});
        });

I am getting this error :

enter image description here

The error is in the line :

callback(currentSession,"created")

What could be the problem?

I already did many researchers but this is a specific case.

Andressa Pinheiro
  • 1,517
  • 2
  • 18
  • 28

1 Answers1

1

Your saveUser() call is missing the setDocNumber argument. It looks like you're not using it in your code though, so you might be able to safely remove it. If you are using it somewhere else (that you haven't shown) then you need to do some argument checking at the top of saveUser() to support optional arguments.

mscdex
  • 104,356
  • 15
  • 192
  • 153