Undefined is not a Function in Node.js using Mongoose

Asked

Viewed 96 times

0

I know that this mistake may have several causes, but I have not been able to know mine. I have a function to save a Document in my Mongodb and I am using callback for this. The code is executed until saving the new document, after that I have an error. The code and the following:

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:

inserir a descrição da imagem aqui

The error is in the following line :

callback(currentSession,"created")

Can someone help me with that?

I’ve done a lot of research but I can’t find an answer.

2 answers

3

I believe your problem is in the amount of parameters passed in the function call, Voce passed four parameters in your call in how much in the same function Voce used 5 parameters.

f(fbUID !== undefined){

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

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

    Wow, I falter. Thank you so much for your answer. I just chose the second one because I found it more complete.:)

  • 2

    We usually focus on function, where we made a mistake and didn’t even notice her call.

2


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

Apparently, 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).

  • That is true. Thank you for your reply. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.