Uncaught TypeError: Cannot set property of undefined

Google Chrome is throwing “Uncaught TypeError: Cannot set property ‘isDown’ of undefined” but it doesn’t look like anything is wrong with my code!

Important part of my variable array:

KEY = {
    UP: 38,
    DOWN: 40,
    W: 87,
    S: 83,
    D: 68
}
pingpong = {
    fps: 60,
    pressedKeys: [],
    complete: false,
}

Key listener initialization (this is where the error is thrown):

    for (var keyCode in KEY) {
        if (KEY.hasOwnProperty(keyCode)) {
            pingpong.pressedKeys[KEY[keyCode]] = {
                isDown: false,
                wasDown: false
            };
        }
    }
    $(document).keydown(function(e) {
        pingpong.pressedKeys[e.which].isDown = true;
    });
    $(document).keyup(function(e) {
/* This line is the issue */    pingpong.pressedKeys[e.which].isDown = false;
    });

Any Ideas?



Uncaught TypeError: Cannot set property ‘position’ of undefined

I have this code giving me the strange error message Uncaught TypeError: Cannot set property ‘position’ of undefined This is the inside of a jQuery plugin to show a google map in a popup. I was using

Uncaught TypeError: Cannot set property ‘selected’ of undefined

This is my JSFiddle My html and js works fine in JSFiddle. But its giving error in the console in my rails app Uncaught TypeError: Cannot set property ‘selected’ of undefined admin.js:1000 Chosen.resu

jQuery Tablesorter Uncaught TypeError: Cannot set property ‘count’ of undefined

I’m using the jQuery TableSorter plugin and get the following error Uncaught TypeError: Cannot set property ‘count’ of undefined with this code: $(document).ready(function () { var copyThead = $(.uiG

Uncaught TypeError: Cannot set property ‘plugin’ of undefined

I am getting the following errors on my website when I load it on an online server. Uncaught TypeError: Cannot set property ‘plugin’ of undefined global.js:1 Port error: Could not establish connection

Javascript: Uncaught TypeError: Cannot set property ‘className’ of undefined

I have a table with an onclick function on it. When clicked upon I get the following error: Uncaught TypeError: Cannot set property ‘className’ of undefined The strange thing is that 3 out of 5 rows t

Uncaught TypeError: Cannot set property ‘0’ of undefined

I keep getting Uncaught TypeError: Cannot set property ‘0’ of undefined, but I can’t for my life figure out what is wrong! var anchor = []; getAnchors(); function getAnchors() { $(‘.anchor’).each(func

Uncaught TypeError: Cannot set property ‘text’ of undefined [closed]

I am getting an error Uncaught TypeError: Cannot set property ‘text’ of undefined on running the following var years= new Array(113); for(i=0;i<113;i++){ years[i][text]=i+1900; years[i][value

Uncaught TypeError: Cannot set property ‘0’ of undefined “

i’m getting the error Uncaught TypeError: Cannot set property ‘0’ of undefined for some reason in this line world_map_array[i][z]=grass.gif|ongrass.gif|collision.gif|above.gif; Why is this happ

Uncaught TypeError: Cannot read property ‘hide’ of undefined

I have an error when trying to load a fancybox on page load. Says Uncaught TypeError: Cannot read property ‘hide’ of undefined on fancybox-pack.js line 18 My code is: $.fancybox($(‘#sample’));

Uncaught TypeError: Cannot set property ‘src’ of undefined

url: http://www.gws-mbca.org The slide show works in Firefox. It used to work in IE and Chrome. Now I get the following error in both IE and Chrome: Uncaught TypeError: Cannot set property ‘src’ of

Answers

e.which is IE only. Real browsers use e.keyCode

jQuery Event Keypress: Which key was pressed?

The problem is that you’re trying to access an element of the pressedKeys array which does not exist. For example, if we pressed the “a” key:

$(document).keyup(function(e) {
    //Pressed "a" so e.which == 65
    pingpong.pressedKeys[e.which].isDown = false;
});

When you initialize your array you only create elements for the properties of the KEY object:

for (var keyCode in KEY) {
    //Iterating over KEY, which contains 5 properties...
    if (KEY.hasOwnProperty(keyCode)) {
        //Add value to pressedKeys (this happens for each of the 5 properties)
        pingpong.pressedKeys[KEY[keyCode]] = {
            isDown: false,
            wasDown: false
        };
    }
}

So pressedKeys only contains 5 elements, corresponding to the properties of KEY. Note that a TypeError is also thrown in the keydown event handler, as well as keyup.

To fix it, you could check that e.which is in the KEYS object in the event handler functions. If it’s not, just ignore it. Something like this perhaps (there may be a better way, this is just what came to mind first):

$(document).keydown(function(e) {
    for(var k in KEY) {
        if(KEY[k] == e.which) {
            break; //Break out of loop and execute last line
        }
        return false; //Key not recognized, last line is not executed
    }
    pingpong.pressedKeys[e.which].isDown = true;
});

A thorough article on the behaviors of different browsers and keypress events: http://unixpapa.com/js/key.html

I’ve found it usefull in the past. Sorry I couldn’t be of more help.