javascript 无法重现类型错误:“未定义”不是对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10077821/
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
Unable to reproduce TypeError: 'undefined' is not an object
提问by John McLear
Google Analytics shows that ~12% of our total users are affected by a Javascript bug of:
谷歌分析显示,我们总用户中约有 12% 受到以下 Javascript 错误的影响:
TypeError: 'undefined' is not an object
TypeError: 'undefined' is not an object
90% of the browsers are Safari 7534.48.3, 10% are Mozilla compatible agent. 75% of the errors come from iPhones, 23% from iPads. 1% from Macintosh, the other 2% is from iPod etc. None of the devices run Linux or Windows.
90% 的浏览器是 Safari 7534.48.3,10% 是 Mozilla 兼容代理。75% 的错误来自 iPhone,23% 来自 iPad。1% 来自 Macintosh,另外 2% 来自 iPod 等。这些设备均不运行 Linux 或 Windows。
I have tried enabling debug mode in safari on both an iPhone and iPad but not able to reproduce the bug.
我曾尝试在 iPhone 和 iPad 上的 safari 中启用调试模式,但无法重现该错误。
Here is a link to a page Google Analytics claims is showing the error. If anyone can consistently reproduce the error here I will be super happy because just a line number would be enough to get me started debugging.
这是一个指向 Google Analytics 声称显示错误的页面的链接。如果有人可以在这里始终重现错误,我会非常高兴,因为只需一个行号就足以让我开始调试。
Can anyone think of any other ways I can try to debug this? Thanks all
谁能想到我可以尝试调试的任何其他方法?谢谢大家
For the curious among us I'm using this code to send errors to GA-- Warning: Possible self promotion.
对于我们中间的好奇,我正在使用此代码向 GA 发送错误-警告:可能的自我宣传。
Update:TypeError: 'undefined' is not an object (evaluating 'safari.self.tab.canLoad')
更新:TypeError: 'undefined' is not an object (evaluating 'safari.self.tab.canLoad')
Managed to get that out of it once when clicking around, mostly on an iphone whilst clicking "Change country.."
单击时设法摆脱了它,主要是在 iPhone 上单击“更改国家/地区..”
Update:Solved this by making sure the element was available in the dom. Turns out the ajax call on success was trying to write to an element that wasn't available.
更新:通过确保元素在 dom 中可用来解决这个问题。事实证明,对成功的 ajax 调用试图写入一个不可用的元素。
I have kept a solid record of Unable to reproduce TypeError: 'undefined' is not an object here
回答by Jon Grant
In your functions.js, you have this:
在你的functions.js 中,你有这个:
storage_get = function(key) {
var store = (window.SAFARI ? safari.extension.settings : localStorage);
var json = store.getItem(key);
if (json == null)
return undefined;
try {
return JSON.parse(json);
} catch (e) {
log("Couldn't parse json for " + key);
return undefined;
}
}
undefined
is NOT a JavaScript keyword. It's a variable that (most of the time) happens to be undefined. You can't use undefined
like this. Consider what would happen if you replaced it with pinkelephant
, as that is the exact thing that's happening here.
undefined
不是 JavaScript 关键字。这是一个(大部分时间)碰巧未定义的变量。你不能这样使用undefined
。考虑一下如果用 替换它会发生什么pinkelephant
,因为这正是这里发生的事情。
回答by Khez
First off, I'm surprised you're worried about your JavaScript pushing errors to GA... You seem to have quite a lot of 404 error codes on a number of links.
首先,我很惊讶您担心您的 JavaScript 将错误推送到 GA...您似乎在许多链接上有相当多的 404 错误代码。
Second off, I found another JavaScript error that I feel merits this answer:
其次,我发现了另一个我认为值得回答的 JavaScript 错误:
Trying to subscribe for email updates gives an undefined variable on response (apparently set to data in the function parameters):
尝试订阅电子邮件更新会在响应中提供一个未定义的变量(显然设置为函数参数中的数据):
msh.header.js:213 ReferenceError: Can't find variable: response