最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: null is not an object (evaluating '*') - Stack Overflow

programmeradmin2浏览0评论

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')

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

Update:

If item.sku is null, the error is:

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

If item is null, the error is:

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

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')

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

Update:

If item.sku is null, the error is:

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

If item is null, the error is:

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

Share Improve this question edited Jul 15, 2019 at 10:49 Icarus 1,6477 gold badges20 silver badges34 bronze badges asked Dec 5, 2017 at 14:07 I'm nidhinI'm nidhin 2,6626 gold badges39 silver badges65 bronze badges 6
  • item or item.sku is null, check them in console – Aleksey Solovey Commented Dec 5, 2017 at 14:12
  • Can you clarify what you're asking? – freginold Commented Dec 5, 2017 at 14:12
  • 1 @freginold I want know in what scenario this error will be thrown – I'm nidhin Commented Dec 5, 2017 at 14:25
  • you lost the reference then (it was deleted?); the referring function was called before the element was parsed into the DOM – Aleksey Solovey Commented Dec 5, 2017 at 14:39
  • What errors do you get if item or item.sku are undefined instead of null? – freginold Commented Dec 5, 2017 at 14:45
 |  Show 1 more comment

2 Answers 2

Reset to default 12

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

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')

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

Based on this answer and 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.

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).

发布评论

评论列表(0)

  1. 暂无评论