Javascript 未捕获的类型错误:无法读取未定义的属性“长度”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3040069/
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 'length' of undefined
提问by ma?ek
I have a plugin that access the lengthproperty on many elements. However, the javascript console points to line 12 of jquery.min.js.
我有一个插件可以访问length许多元素的属性。但是,javascript 控制台指向 jquery.min.js 的第 12 行。
How can I backtrace to find the offending line in my plugin?
如何回溯以在我的插件中找到违规行?
采纳答案by Sarfraz
How can I backtrace to find the offending line in my plugin?
如何回溯以在我的插件中找到违规行?
firebugis great way to debug those errors.
firebug是调试这些错误的好方法。

(source: getfirebug.com)

(来源:getfirebug.com)
回答by s3m3n
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).
如果您使用缩小的脚本,任何调试器(就像完全最好的萤火虫)都会向您显示相同的有问题的行,而这些信息是无用的(缩小的脚本难以阅读和理解,它们被写在 1 行中)。
Few ways to solve problems like this:
解决此类问题的几种方法:
- As told before me: for developing use not minified scripts, debugger will show you the line that means something and you if you are lucky you can find very useful comments of developers.
- If you can't find full version of the script use unminifier like this one: http://jsbeautifier.org/(paste minified script and click button below). Add to your project uminified script and run invalid function again. Debugger again will show you the line, but this time you will see a real logic line and you can understand what is the problem in most cases.
- Debugger will show you which script throws problem. Check if there any any new versions of this script. I had the same problem once, found line of the minified script, name of plugin (few lines above in copyrights) and then found that there is a new version available. Reviewed changelog and there was: "Added multiple 'sanity checks' throughout the code for potential unknown attribute values" - headshot :) Updated script and everything was fine from now without special debugging taking hours.
- Google your error with script name - it helped me so many times.. Probably you did it, but maybe you didn't try with speech marks "" - google will return pages with exact phrase in text.
- 正如我之前所说:对于开发使用而不是缩小的脚本,调试器会向您显示有意义的行,如果幸运的话,您可以找到开发人员非常有用的评论。
- 如果您找不到完整版本的脚本,请使用像这样的 unminifier:http://jsbeautifier.org/ (粘贴缩小后的脚本并单击下面的按钮)。添加到您的项目 uminified 脚本并再次运行无效功能。Debugger 会再次向您显示该行,但这次您将看到一条真正的逻辑行,并且您可以了解大多数情况下是什么问题。
- 调试器将向您显示哪个脚本引发问题。检查此脚本是否有任何新版本。我曾经遇到过同样的问题,找到了缩小脚本的行,插件名称(版权中的上面几行),然后发现有一个新版本可用。了变更日志,有:“在整个代码中为潜在的未知属性值添加了多个‘健全性检查’” - 爆头 :) 更新了脚本,从现在开始一切都很好,无需花费数小时进行特殊调试。
- 用脚本名称谷歌你的错误 - 它帮助了我很多次......可能你做到了,但也许你没有尝试使用语音标记“” - 谷歌将返回带有文本中精确短语的页面。
回答by Kevin Beal
If you use Chrome you can use the built in developer tools (which I prefer to Firebug) by going to "View > Developer > Developer Tools". 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.
如果您使用 Chrome,您可以通过转到"View > Developer > Developer Tools". 控制台中的错误之前通常会有一个向右的小箭头,单击该箭头时会显示有关错误的更多详细信息。
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. The problem is in your code. It's just that jQuery can't use undefined variables you pass to it.
您不可能通过查看 jQuery 的源代码来找出问题所在。问题出在您的代码中。只是 jQuery 不能使用你传递给它的未定义变量。
回答by Colin Scott-Fleming
I often find that these ambiguous framework errors are the result of an AJAX request error. 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.
我经常发现这些模棱两可的框架错误是 AJAX 请求错误的结果。如果是这种情况,您选择的开发人员工具很可能包含一个网络选项卡,这可能会突出显示错误的真正来源。
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 请求的结果,则格式化和处理错误都是额外的且经常被忽视的步骤。
回答by tainterr
In addition to the debugger/firebug, a simple way to track this down (if you have a sandboxed version of the site) is to comment out each plugin individually and check if the error goes away.
除了调试器/firebug 之外,一个简单的跟踪方法(如果您有站点的沙盒版本)是单独注释掉每个插件并检查错误是否消失。

