简体   繁体   中英

Cannot read property '0' of undefined

I have a grid that loads and returns JSON items. A radio button filter has been added to filter those items. Every time the radio button is selected, the grid items will be refreshed to display items that fit the filtering criteria.

THe thing is, when I click the radio button, nothing happens and I see a Cannot read property '0' of undefined error. The JSON loads correctly and there aren't any 404 or 500 errors, which I can confirm using Chrome DevTools. The offending line of code appears to be while (typeof n.main[l.toString()] != "undefined") . Below is the function containing said code:

function j(n) {
var m = [],
    l;
if (n.main != "undefined") {
    l = 0;
    while (typeof n.main[l.toString()] != "undefined") {
        m.push(n.main[l.toString()]);
        l++
    }
}
if (GridApp.logging) {
    $.log(m.length + " talks loaded")
}
m = f(m);
m = GridMathFactory.applyGridMath(m);
GridModel.setTalkVOs(m);
d = e(d);
for (l = 0; l < d.length; l++) {
    d[l].talkIdx = l
}
    GridController.onTalksLoadSuccess()
}

Here's the code for function n :

function n() {
if (GridApp.logging) {
    $.log("GridMathFactory.calculateSubColumns()")
}
var B = 0;
var A;
for (var z = 0; z < q.length; z++) {
    var y = columnScale - 1;
    if (t[y] < f.length) {
        A = g(q[z], B)
    }
    B += (A) ? k : k;
    if (GridApp.logging) {
        $.log("Just calculated column " + z + "; Contained 1 ? " + A)
        }
    }
}
if (n.main != "undefined") {

should be:

if (n.main !== undefined) {

or:

if (typeof n.main != "undefined") {

Turns out main was something that didn't exist in the JSON I was calling. Removed it and now it works as it should.

function j(n) {
var m = [],
    l;
if (n != "undefined") {
    l = 0;
    while (typeof n[l.toString()] != "undefined") {
        m.push(n[l.toString()]);
        l++
    }
}
if (GridApp.logging) {
    $.log(m.length + " talks loaded")
}
m = f(m);
m = GridMathFactory.applyGridMath(m);
GridModel.setTalkVOs(m);
d = e(d);
for (l = 0; l < d.length; l++) {
    d[l].talkIdx = l
}
    GridController.onTalksLoadSuccess()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM