简体   繁体   English

未捕获的TypeError:无法读取未定义的属性“长度”

[英]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: 几乎没有办法解决这样的问题:

  1. 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. 正如我之前所说的那样:为了开发使用而不是缩小脚本,调试器将向您显示意味着什么的行,如果您很幸运,您可以找到非常有用的开发人员评论。
  2. 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). 如果你找不到完整版本的脚本,可以使用unminifier,如下所示: http ://jsbeautifier.org/(粘贴缩小的脚本并点击下面的按钮)。 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. 调试器将再次显示该行,但这次您将看到一条真正的逻辑行,您可以在大多数情况下了解问题所在。
  3. 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. 审核了更改日志,并且:“ 在整个代码中为潜在的未知属性值添加了多个'健全性检查' ” - headshot :)更新了脚本,从现在开始一切都很好,无需特殊调试花费数小时。
  4. 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. 谷歌你的错误与脚本名称 - 它帮了我很多次..可能你做了,但也许你没有尝试语音标记“” - 谷歌将返回文本中的确切短语页面。

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.

相关问题 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined 未捕获的TypeError:无法读取未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined Uncaught TypeError:无法读取未定义的属性“ length”…? - Uncaught TypeError: Cannot read property 'length' of undefined…? 未捕获的类型错误:无法读取未定义的属性“长度” - Uncaught TypeError : Cannot read property 'length' of undefined Uncaught TypeError:无法读取未定义的属性“ length”? - Uncaught TypeError: Cannot read property 'length' of undefined? 未捕获的TypeError:无法读取未定义的属性“长度”(…) - Uncaught TypeError: Cannot read property 'length' of undefined(…) 无法读取未定义的Uncaught TypeError的属性“ length”: - Cannot read property 'length' of undefined Uncaught TypeError:? TypeError:a未定义+未捕获TypeError:无法读取未定义的属性“ length” - TypeError: a is undefined + Uncaught TypeError: Cannot read property 'length' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM