[英]Uncaught TypeError: Cannot read property 'length' of undefined
I have a plugin that access the length
property on many elements. 我有一个插件,可以访问许多元素的length
属性。 However, the javascript console points to line 12 of jquery.min.js. 但是,javascript控制台指向jquery.min.js的第12行。
How can I backtrace to find the offending line in my plugin? 如何回溯以查找插件中的违规行?
If you use minified scripts any debugger (like totally the best firebug) will show you the same problematic line and this information is useless (minified scripts are hard to read and understand and they are written in 1 line). 如果您使用缩小的脚本,任何调试器(如完全最好的firebug)将向您显示相同的有问题的行,并且此信息是无用的(缩小的脚本难以阅读和理解,并且它们写在一行中)。
Few ways to solve problems like this: 几乎没有办法解决这样的问题:
How can I backtrace to find the offending line in my plugin? 如何回溯以查找插件中的违规行?
firebug is great way to debug those errors. firebug是调试这些错误的好方法。
(source: getfirebug.com ) (来源: getfirebug.com )
If you use Chrome you can use the built in developer tools (which I prefer to Firebug) by going to "View > Developer > Developer Tools"
. 如果您使用Chrome,可以转到"View > Developer > Developer Tools"
使用内置的开发人员工具(我更喜欢Firebug)。 The error in the console there will often have a small rightward pointing arrow before it which when clicked will show more details about the error. 控制台中的错误通常会在它之前有一个小的向右箭头,点击它时会显示有关错误的更多详细信息。
Javascript errors are not likely to originate in a library, but instead the code that references the methods/functions inside that library and so you want to look through all the problem lines listed on the right side of the console and select the line that corresponds to the code you wrote which will be where your problem is. Javascript错误不太可能来自库,而是引用该库中的方法/函数的代码,因此您希望查看控制台右侧列出的所有问题行并选择对应的行您编写的代码将是您的问题所在。
You are not likely to figure out where your problem is by looking through jQuery's source code. 通过查看jQuery的源代码,您不太可能找出问题所在。 The problem is in your code. 问题出在您的代码中。 It's just that jQuery can't use undefined variables you pass to it. 只是jQuery不能使用传递给它的未定义变量。
I often find that these ambiguous framework errors are the result of an AJAX request error. 我经常发现这些模糊的框架错误是AJAX请求错误的结果。 If that is the case, your Developer Tool Of Choice most likely contains a Network tab, and that may highlight the real source of the error. 如果是这种情况,您的Developer Tool Of Choice很可能包含一个Network选项卡,这可能会突出显示错误的真正来源。
If you are using jQuery (or any JavaScript framework) to process the results of AJAX requests, both formatting and handling errors are additional and often overlooked steps. 如果您使用jQuery(或任何JavaScript框架)来处理AJAX请求的结果,则格式化和处理错误都是额外的且经常被忽略的步骤。
除了调试器/ firebug之外,跟踪它的一种简单方法(如果你有一个沙盒版本的网站)是单独注释掉每个插件并检查错误是否消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.