javascript 未捕获的类型错误:无法读取未定义的属性 x
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19289425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Cannot read property x of undefined
提问by Nick Germi
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。
回答by Abhidev
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 = data[0].weekending.length
which 等于 10 并且您正在迭代data[0].jobs
只有 3 个作业的对象。您对作业进行了 3 次以上的迭代,因此出现错误。
checkout the fiddle http://jsfiddle.net/wJUeP/7/
回答by jasonscript
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