John Lim
John Lim

Reputation: 3109

TypeError: Cannot call method 'toLowerCase' of undefined

I ran into an error and I don't know why. I try to transform all the character into lowercase before inserting into database and for comparison purposes. At the first function this.addUser I call toLower to transform it become lowercase it has no problem but at second function this.checkPublicUsername I call toLower it display an error in my terminal said that TypeError: Cannot call method 'toLowerCase' of undefined

function toLower (text) {
    return text.toLowerCase();
}

this.addUser = function(data,callback) {
    "use strict";
    // Generate password hash
    var salt = bcrypt.genSaltSync();
    var password_hash = bcrypt.hashSync(data.password, salt);
    // transform all character into lowercase
    var real = toLower(data.publicUsername);
    // Create user document
    var user = {
        '_id': data.email,
        'email':data.email,
        'password': password_hash,
        'name':{
            'firstName': data.firstName,
            'lastName': data.lastName
        },
        'penName': data.penName,
        'publicUsername':{
            'display':data.publicUsername,
            'real':real
        }
    };
    users.insert(user, function (err, result) {
        "use strict";

        if (!err) {
            console.log("Inserted new user");
            return callback(null, result[0]);
        }
        if (err) console.log(err)

        return callback(err, null);
    });
}

this.checkPublicUsername = function(data,callback) {
    "use strict";
    var real = toLower(data);
    var publicUsername = {
        "publicUsername.real":real
    }
    users.find(publicUsername).count(function(err, result) {
        callback(null,result);
    })

}

call checkPublicUsername

 users.checkPublicUsername(data.publicUsername, function(err, result){
     if(result){
     callback(new Error('"'+data.publicUsername+'" has been taken. Please choose another.'), null);
             }
        });

Upvotes: 3

Views: 26231

Answers (2)

John Lim
John Lim

Reputation: 3109

The reason i get this error is because data.publicUsername is undefined at the time i try to perform this function

this.checkPublicUsername = function(data,callback) {
    "use strict";
    var real = toLower(data);
    var publicUsername = {
        "publicUsername.real":real
    }
    users.find(publicUsername).count(function(err, result) {
        callback(null,result);
    })

}

the real is undefined is causing the error, if you want to avoid this problem just simply write a if else so if the real is undefined then don't perform this function

 users.find(publicUsername).count(function(err, result) {
            callback(null,result);
        })

the if else can be something like this

 if(data != undefined){
            var real = toLower(data);
        }

hope this help !

Upvotes: 2

Shai Aharoni
Shai Aharoni

Reputation: 1957

You get this error because data.publicUsername is undefined, check the value that you are passing to this function.

Upvotes: 5

Related Questions