Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Moira Lawrie-Martyn
Moira Lawrie-Martyn
8,073 Points

TypeError: Cannot read property 'toUpperCase' of undefined.... but it works in JSFiddle?

Hi guys, I'm creating a site in Node.JS and one of the things I'm attempting to do is compare a search input string with a CSV file which is acting as my database.

I'm trying to remove the case sensitivity as you can see below:

function matchData(searchData){
    var fileContent = [];
    var results = new Array();
    fileContent = fs.readFileSync('./data/database.csv', 'utf8').split('\n');

    for(var line of fileContent){
        var result = line.split(',');

        var check = result[5].toUpperCase();
        console.log(check);
        var test = searchData.toUpperCase();
        console.log(test);

        if(check === test)
        {
            results.push(result);
        }
    }
    return results;
}

The crazy thing is, the console.log(check) and console.log(test) both spit out the uppercase strings so it should work, but every time I run it I get "TypeError: Cannot read property 'toUpperCase' of undefined". I tried toLowerCase() instead and it did exactly the same thing.

And if I type the string in case sensitive, I know the function works perfectly.

Can anyone tell me what I'm doing wrong?

Carl Evison
Carl Evison
2,656 Points

Since you're doing a strict comparison can you do

 console.log(typeof check);

 console.log(typeof test);

To make sure they're both of the same kind.

2 Answers

Moira Lawrie-Martyn
Moira Lawrie-Martyn
8,073 Points

Okay, I figured it out. I had a stray empty line going into the function that was causing errors. So I've put in an if(line != "") and that's fixed it :)

Carl Evison
Carl Evison
2,656 Points

Glad you got it sorted :)