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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You are totally right. Thank you. That was the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.