javascript 类型错误:null 不是对象(评估 '*')

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47655564/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:32:05  来源:igfitidea点击:

TypeError: null is not an object (evaluating '*')

javascriptangularjsjson

提问by I'm nidhin

For the below code:

对于以下代码:

var item = cartModel.getlist()[index];
if((item.isDepo()) {
    // Some code
} else if(!permission.hasPermissionToVoidSKU()) {
    // Some code
} else if(item.sku.indexOf(mposConstants.RESTOCK_FEE_SKU) > -1){
                // Some code 
}

I'm getting this error:

我收到此错误:

TypeError: null is not an object (evaluating 'item.sku.indexOf')

类型错误:null 不是对象(正在评估“item.sku.indexOf”)

If item object is null, the error is something different (see below). In what scenario will this error be thrown?

如果 item 对象为 null,则错误有所不同(见下文)。在什么情况下会抛出这个错误?

Update:

更新:

If item.skuis null, the error is:

如果item.sku为空,则错误为:

[FATAL] [] [-] ["TypeError: Cannot read property 'indexOf' of null

[致命] [] [-] ["TypeError: 无法读取 null 的属性 'indexOf'

If itemis null, the error is:

如果item为空,则错误为:

[FATAL] [] [-] ["TypeError: Cannot read property 'isDepo' of null

[致命] [] [-] ["TypeError: 无法读取 null 的属性 'isDepo'

回答by Michael Johansen

The reason for the different error messages is quite simply that they are produced by different browsers. The error is the same (skuon the object itemis null).

出现不同错误消息的原因很简单,因为它们是由不同的浏览器产生的。错误是相同的(sku在对象上itemnull)。

Given the following code

鉴于以下代码

<script>
  var item = {sku: null};
  item.sku.indexOf("");
</script>

here is some error messages for different browsers:

以下是针对不同浏览器的一些错误消息:

  • Firefox: TypeError: item.sku is null
  • Firefox Developer Edition: TypeError: item.sku is null, can't access property "indexOf" of it
  • Opera: TypeError: Cannot read property 'indexOf' of null at example.html:3
  • Safari: TypeError: null is not an object (evaluating 'item.sku.indexOf')
  • 火狐: TypeError: item.sku is null
  • 火狐开发者版: TypeError: item.sku is null, can't access property "indexOf" of it
  • 歌剧: TypeError: Cannot read property 'indexOf' of null at example.html:3
  • 苹果浏览器: TypeError: null is not an object (evaluating 'item.sku.indexOf')

To get the error message you have gotten, itemmust be defined as an object, and skumust be set to null. If skuwere undefined, you would have gotten an error message like this (Safari): TypeError: undefined is not an object (evaluating 'item.sku.indexOf'). If itemwas null, you would have gotten something like this: TypeError: null is not an object (evaluating 'item.sku').

要获得你得到的错误信息,item必须定义为一个对象,并且sku必须设置为null。如果skuundefined,您会收到这样的错误消息 (Safari): TypeError: undefined is not an object (evaluating 'item.sku.indexOf')。如果itemnull,你会得到这样的事情:TypeError: null is not an object (evaluating 'item.sku')

回答by freginold

Based on this answerand others, it sounds like you're getting that error because a function is called before the DOM element it references or acts upon has been loaded.

根据this answer和其他答案,听起来您收到该错误是因为在加载它引用或作用于其上的DOM元素之前调用了一个函数。

In the snippet of code you provided, I don't see a direct reference to any DOM elements, but I would suggest calling your script after your HTML has finished rendering (i.e. by putting any <script>tags at the end of your HTML, or by using a $(document).ready()call if you use jQuery).

在您提供的代码片段中,我没有看到对任何 DOM 元素的直接引用,但我建议在您的 HTML 完成渲染后调用您的脚本(即通过<script>在 HTML 的末尾放置任何标签,或使用一个$(document).ready()电话,如果你使用jQuery)。