Maybe bug and fix: aoColumns Null handling mData and mRender

Maybe bug and fix: aoColumns Null handling mData and mRender

burncharburnchar Posts: 118Questions: 12Answers: 0
edited October 2012 in Editor
DT: 1.9.3, JQuery: 1.8.2

I've been hunting down a JavaScript error where, when displaying comma-separated values in a column as in the Editor Joined Tables example, I was getting:
[code]"Unable to get value of the property 'length'"[/code] on line 754 of jquery.dataTables.js 1.9.3, which nearby reads:

[code]
// Traverse each entry in the array getting the properties requested
for (var j = 0, jLen = data.length; j < jLen; j++) { // line 754
out.push(fetchData(data[j], type, innerSrc));
}
[/code]

tl;dr: this loop breaks if the object is null.

Specifically, if I used mRender to comma-separate values from the aaData object like so:
[code]
{"mData":"ROTATION"},
{
"mData":"EQUIPMENT",
"mRender":"[, ].name"
},
{"mData":"RECIPE_NOTES"}

[/code]
I would get the error. If I removed the lines for "EQUIPMENT" I'd get no error (and no column).

My aaData looks something like this (generated in C# by MS Web API using JSON.Net):
[code]
{
"id": 1,
"error": "",
"fieldErrors": [],
"data": [],
"aaData": [{
"DT_RowId": "row_2436",
// More columns...
"EQUIPMENT": [
{"id": "4", "name": "foo"},{"id": "5","name": "bar"}, {"id": "7","name": "BOSE speakers suck"}
]},
[/code]

If that EQUIPMENT line is changed to: [code]"EQUIPMENT": null[/code] (which JSON.Net generates when the C# object is null), it breaks with the error above.

I naïvely put "alert(data);" in the loop and saw a lot of pop-ups with the text: [code][object Object],[object Object][/code] which looked fine. I guessed that it might be a null handling problem and found a fix ("hack" is probably a better word):
[code]
Change:
for (var j = 0, jLen = data.length; j < jLen; j++) { ...
To:
if(data) for (var j = 0, jLen = data.length; j < jLen; j++) { ...
[/code]

This gives me correct results in my limited testing even for null values described above.

Replies

  • allanallan Posts: 63,602Questions: 1Answers: 10,486 Site admin
    That's interesting. Yes I hasn't anticipated a null value when it has been told to expect an array, but it does seem like a valid situation. I'll look at integrating this in as part of 1.10 - thanks for flagging it up.

    Allan
This discussion has been closed.