undefined is not a function in Node.js using Mongoose

0

I know that this error can have several causes, but I can not know so far. I have a function to save a Document in my MongoDB and I am using callback for this. The code is executed until the new document is saved, after that I have an error. The code is as follows:

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 through the function below:

f(fbUID !== undefined){

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

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

But I continue with the following error:

Theerrorisinthefollowingline:

callback(currentSession,"created")

Can anyone help me with this?

I've done a lot of research but can not find an answer.

    
asked by anonymous 22.07.2014 / 20:39

2 answers

2

Its saveUser function waits for 5 parameters. When calling it, you just passed 4. The 5th - callback - is therefore undefined , so when you try to call it as function it throws the error "undefined is not a function".

Obviously, the missing parameter is setDocNumber , pass it to the function, and the problem must be solved. Or remove it if it is not relevant (note that it is not used anywhere in your code).

    
22.07.2014 / 20:52
3

I think your problem is in the number of parameters passed in the function call, you have passed four parameters in your call, but in the same function you used 5 parameters.

f(fbUID !== undefined){

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

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