Ticket #2568 (closed bug: fixed)
elem has no properties error in nodeName method
| Reported by: | hobbit | Owned by: | |
|---|---|---|---|
| Priority: | major | Milestone: | 1.2.4 |
| Component: | core | Version: | 1.2.3 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
When we run the $.getJSON method sometimes an unusual issue rise in. "elem has no properties in 660 line."
nodeName: function( elem, name ) {
return elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase();
}
vs
nodeName: function( elem, name ) {
return elem && elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase();
}
Change History
comment:2 Changed 4 years ago by dmethvin
- Status changed from new to closed
- Resolution set to invalid
Since this was reported against 1.2.3 and never got a test case, I'm closing it.
comment:3 Changed 4 years ago by Jacob W
- Status changed from closed to reopened
- Resolution invalid deleted
I just encountered this error as well - it manifests any time I try to load a JSON object containing an empty array, eg
{ "response": {
"name":"testcase", "properties": [ ]
} }
Unless I've misunderstood, this is a valid object; it's certainly understood by javascript when not using jQuery and JSON Lint likes it. I'm guessing that the error appears when trying to run nodeName on the first member of the array, which obviously doesn't exist.
My fix is:
try {
return elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase();
} catch (e if e instanceof TypeError) {
return false;
}
I'm assuming that this will be better than checking that elem.nodeName is defined, since this error doesn't seem common. I'm not an expert on the inner working of jQuery, however, so this might be better fixed where the function is called.
Jacob.
comment:4 Changed 4 years ago by dmethvin
Hey, I should close stale tickets more often. Jacob, do you have a snippet of the ajax call you make?
comment:5 Changed 4 years ago by Jacob W
It's just $.getJSON(path,function(response). The error occurs immediately, and code in the function isn't executed. It's loading a document that looks like this:
{ "response":{ "totalresults":"2588", "first":"0", "length":"15", "items":[ {
"titre": "TITLE", "sid":"2",
"instruments": "", "duree": "6:33",
"lmoc":"1", "fav":"0", "weight":"2587"
},{
"titre": "TITLE, "sid":"3",
"instruments": "", "duree": "7:02",
"lmoc":"1", "fav":"0", "weight":"2586"
}]
} }
Any time items is empty - i.e. "items":[] - I get this error.
In fact the "fix" I suggested above doesn't work - IE and Firefox are both fine with it, but in Safari it results in a "jQuery not found" error when the page loads!
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Test case please ?