Getting a "Cannot read property 'myVar' of undefined" that I can't resolve even with typeof

myWord = ''

db.collection('testCollection', function (err, collection) {
    collection.find({room:"room1"}).toArray(function(err, results) {
        if(typeof results[0].word === "undefined")
            myWord = '';
        else
            myWord = results[0].word;
    });
});

I'm using socket.io, express, and the mongodb native driver.

The database is 100% empty when this query executes. Every time it hits the line:

if(typeof results[0].word === "undefined")

I get

TypeError: Cannot read property 'word' of undefined

I'm new to mongodb and nodejs, so I might even be structuring my code wrong. If you need to see more code, of course let me know.

Try if(results[0] === undefined) or if(typeof results[0] == "undefined")

You're trying to take the type of what you get after dereferencing, when it's the dereferencing of a null value that's breaking it in the first place.