Sunday, 11 June 2017

Cannot add properties to an object - Node.js/Mongoose

I am trying to check if a user account has already been created in either User model or TempUser model, however encounter an issue where I cannot add a found value to an object in order to process the conditional check.

Please check the code below and comments:

exports.isRegistered = function(userModel, tempUserModel, username){
    var obj = {}; //creating an empty object to add the found results later
    userModel.findOne({username: username}, function(err, user){
        if(err) throw err;
        if(user){
            obj.user = user;
            console.log(obj); // doesn't fire as no user was found found
        }else{
            tempUserModel.findOne({username: username}, function(err, tempUser){
                if(err) throw err;
                obj.tempUser = tempUser;
                console.log(obj); // returns object with a tempUser as it was found
            });
        }
    });
    console.log(obj); //returns an empty object again.
};



via Emil Gurbanov

No comments:

Post a Comment