未捕获类型错误:无法读取未定义的属性x

时间:2022-08-24 13:50:55

Getting the above error in Chrome console while the actual script works and generates the right output, wonder how I can get rid of this error and what is causing it.

当实际脚本工作并生成正确的输出时,在Chrome控制台中获取上面的错误,我想知道如何消除这个错误以及导致这个错误的原因。

JSFiddle: http://jsfiddle.net/wJUeP/

JSFiddle:http://jsfiddle.net/wJUeP/

HTML Code:

HTML代码:

<ul id="menu"></ul>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

JS Code:

JS代码:

$(function(){
var data = [{"weekending":"09\/10\/2013","jobs":[{"jobnumber":"1001","jobaddress":"Test1001","employees":[{"employeenumber":"1","name":"James Blabla","class":"FHM","notes":"xx","nt-wkmon":"2","t12-wkmon":"5","dt-wkmon":"4","status-wkmon":"Public Holiday","startTime-wkmon":"4","finishTime-wkmon":"6","nt-wktue":"7"}]},{"jobnumber":"1002","jobaddress":"Test1002","employees":[{"employeenumber":"1","name":"Cameron Le","class":"FHQ","notes":"xx","nt-wkmon":"2","t12-wkmon":"5","dt-wkmon":"4","status-wkmon":"Public Holiday","startTime-wkmon":"4","finishTime-wkmon":"6","nt-wktue":"7"},{"employeenumber":"2","name":"David Le","class":"FHQ","notes":"xx","nt-wkmon":"2","t12-wkmon":"5","dt-wkmon":"4","status-wkmon":"Public Holiday","startTime-wkmon":"4","finishTime-wkmon":"6","nt-wktue":"7"}]},{"jobnumber":"1003","jobaddress":"Test1003","employees":[{"employeenumber":"1","name":"Nick G","class":"sdf","notes":"sdf","nt-wkmon":"2","t12-wkmon":"5","dt-wkmon":"4","status-wkmon":"Public Holiday","startTime-wkmon":"4","finishTime-wkmon":"6","nt-wktue":"7"}]}]}];

for(var i = 0, j = data[0].weekending.length; i<j; i++) {
    rootMenu = data[0].jobs[i];
    $("#menu").append("<li id='job_" + rootMenu.jobnumber + "'>" + rootMenu.jobnumber);
    if(rootMenu.hasOwnProperty("employees")) {
        $("#menu").append("<ul id='employees_job_" + rootMenu.jobnumber + "'>");
        for(var n = 0, m = rootMenu.employees.length; n<m; n++) {
            var subMenu = rootMenu.employees[n];
            if(subMenu.hasOwnProperty("name")) {
                $("#employees_job_" + rootMenu.jobnumber).append("<li>" + subMenu.name + "</li>");
            }
        }
        $("#menu").append("</ul>");
    } else {
        $("#menu").append("</li>");
    }
}
});

Note: I'm still in the development stage of my application and I have the flexibility to change and manipulate the data structure, if embedded JSON data looks bad I can change it, actual data is stored in a XML file and then read by PHP and outputted as JSON.

注意:我还在应用程序的开发阶段,我有修改和操作数据结构的灵活性,如果嵌入的JSON数据看起来很糟糕,我可以修改它,实际数据存储在XML文件中,然后由PHP读取并输出为JSON。

2 个解决方案

#1


1  

the for loop condition is incorrect.

for循环条件不正确。

you have used j = data[0].weekending.length which is equal to 10 and you are iterating over the data[0].jobs object which has only 3 jobs. You are iterating more than 3 times over the jobs and hence you are getting the error.

您已经使用了j =数据[0].weekending。长度等于10,你在数据[0]上迭代。jobs对象,只有3个作业。您在作业上迭代了3次以上,因此您将得到错误。

checkout the fiddle http://jsfiddle.net/wJUeP/7/

结帐小提琴http://jsfiddle.net/wJUeP/7/

#2


0  

The error is here:

这里的错误是:

j = data[0].weekending.length

This is returning 10, which is the length of the string in the weekending property == 10 ("09/10/2013") I think you need this instead:

这是返回10,即周结束属性中的字符串长度= 10(“09/10/2013”)

j = data[0].jobs.length

#1


1  

the for loop condition is incorrect.

for循环条件不正确。

you have used j = data[0].weekending.length which is equal to 10 and you are iterating over the data[0].jobs object which has only 3 jobs. You are iterating more than 3 times over the jobs and hence you are getting the error.

您已经使用了j =数据[0].weekending。长度等于10,你在数据[0]上迭代。jobs对象,只有3个作业。您在作业上迭代了3次以上,因此您将得到错误。

checkout the fiddle http://jsfiddle.net/wJUeP/7/

结帐小提琴http://jsfiddle.net/wJUeP/7/

#2


0  

The error is here:

这里的错误是:

j = data[0].weekending.length

This is returning 10, which is the length of the string in the weekending property == 10 ("09/10/2013") I think you need this instead:

这是返回10,即周结束属性中的字符串长度= 10(“09/10/2013”)

j = data[0].jobs.length