Before I explain this, this is not really a bug report and not something that will happen with Drupal itself - but it is a good safe-guard to have for data integrity and preventing errors. I'm getting this error when embedding a Javascript widget from a third-party on my site (the Twitter timeline widget, to be exact). I'm not exactly sure why, but it seems to be adding something to the AJAX response.
Here's the code in question:
for (var i in response) {
if (response[i]['command'] && this.commands[response[i]['command']]) {
this.commands[response[i]['command']](this, response[i], status);
}
}
For reasons unknown, the widget is adding an undefined item to response, so we get the error: Uncaught TypeError: Cannot read property 'command' of undefined
A good solution to avoid these types of errors, is to simple check to make sure that the response exists:
for (var i in response) {
if (response[i] != undefined) {
if (response[i]['command'] && this.commands[response[i]['command']]) {
this.commands[response[i]['command']](this, response[i], status);
}
}
}
I'm not the best with JS, but I think that's a good solution. I can provide a patch if this seems like an addition worth making to core. It'll probably never happen with Drupal itself, but why not take a step to prevent it, either by Drupal or external JS code.