简体   繁体   English

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

[英]Uncaught TypeError: Cannot read property 'timing' of undefined

How do I properly do object detection for window.performance.timing ? 如何正确检测window.performance.timing对象?

An unknown version of Chrome being run by Googlebot is spawning the following error: 由Googlebot运行的未知版本的Chrome产生以下错误:

Uncaught TypeError: Cannot read property 'timing' of undefined 未捕获的TypeError:无法读取未定义的属性“ timing”

The only instances of window.performance.timing are in the following snippets of code: window.performance.timing的唯一实例在以下代码片段中:

else if (
window.performance!=undefined
&& window.performance.timing!=undefined
&& window.performance.timing.toJSON!=undefined) {/* etc */}

Obviously regardless of my efforts to do object detection Googlebot is somehow still spawning the error message. 显然,无论我为进行对象检测所做的任何努力,Googlebot仍会以某种方式生成错误消息。 I can not use try and catch and I have zero instances in my JavaScript error log of this happening in any testable (eg Chrome itself) browser, only Googlebot. 无法使用try and catch并且我的JavaScript错误日志中有零个实例,这种情况发生在任何可测试的浏览器(例如Chrome本身)中,只有Googlebot。

Try this way. 尝试这种方式。

else if (
window.performance
&& window.performance.timing
&& window.performance.timing.toJSON) {/* etc */}

And as I know, you should use !== instead of != 据我所知,您应该使用!==而不是!=

If window.performance is undefined the if statement will still check the other two expressions. 如果未定义window.performance则if语句仍将检查其他两个表达式。 If there is no window.performance it can't check for window.performance.timing . 如果没有window.performance ,则无法检查window.performance.timing

At least that's what I am guessing. 至少那是我的猜测。 I would try to nest the if statements: 我会尝试嵌套if语句:

if(window.performance != undefined) {
  if(window.performance.timing != undefined) {
    if(window.performance.timing.toJSON != undefined) {
      /* etc */
    }
  }
}

What if window.performance is null ? 如果window.performancenull怎么办? I bet you would get the same error message. 我敢打赌,您会收到相同的错误消息。

That's why the canonical way to check objects existence is: 这就是为什么检查对象存在的规范方法是:

if (window.performance && window.performance.whatever && ...) ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法读取未定义的属性“未定义” - Uncaught TypeError: Cannot read property 'undefined' of undefined 未捕获的TypeError:无法读取未定义的属性“数量” - Uncaught TypeError: Cannot read property 'quantity' of undefined 未捕获的TypeError:无法读取未定义的属性'fromJSON' - Uncaught TypeError: Cannot read property 'fromJSON' of undefined 未捕获的TypeError:无法读取未定义的属性'formatter' - Uncaught TypeError: Cannot read property 'formatter' of undefined Uncaught TypeError:无法读取未定义的属性“ stopVideo” - Uncaught TypeError: Cannot read property 'stopVideo' of undefined 未捕获的类型错误:无法读取未定义的属性“setCrossOrigin” - Uncaught TypeError: Cannot read property 'setCrossOrigin' of undefined 未捕获的TypeError:无法读取未定义的属性'NaN' - Uncaught TypeError: Cannot read property 'NaN' of undefined 未捕获的TypeError:无法读取未定义的属性'getAttribute' - Uncaught TypeError: Cannot read property 'getAttribute' of undefined 未捕获的TypeError:无法读取未定义的属性“地理编码” - Uncaught TypeError: Cannot read property 'geocode' of undefined 未捕获的TypeError:无法读取未定义的属性“时间戳” - Uncaught TypeError: Cannot read property 'timestamp' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM