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.

CommentFileSizeAuthor
#3 core-ajax-check-response-property.patch805 bytescaiosba
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nod_’s picture

Category: bug » feature

If it's not a bug, don't label it as a bug :)

This is fixed in D8 so leaving for D7.

modestmoes’s picture

I've run into this JS alert a few times in D7. It would be great if this was fixed for Drupal7.

caiosba’s picture

Status: Active » Needs review
FileSize
805 bytes

I also think that this should be fixed. The attached patch applies on D7 the same fix that was applied on D8.

infojunkie’s picture

Status: Needs review » Reviewed & tested by the community

#3 works for me, thanks.

modestmoes’s picture

#3 works for me too.

David_Rothstein’s picture

Category: feature » bug
Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.