简体   繁体   English

为什么我会收到Uncaught TypeError:无法在分配时读取未定义的属性“ 0”?

[英]why am I getting Uncaught TypeError: Cannot read property '0' of undefined on assign?

I have: 我有:

var lineItemTracker = new Array();
lineItemsTracker[i]['sales_order_line_item_id'] = result[i].sales_order_line_item_id;

This throws the error 这引发错误

Uncaught TypeError: Cannot read property '0' of undefined on assign

if I check 如果我检查

console.log(result[i].sales_order_line_item_id);

then I get the correct result. 然后我得到正确的结果。 When I check 当我检查

console.log('here ' + lineItemsTracker[i]['sales_order_line_item_id']);

I get the error message. 我收到错误消息。

lineItemsTracker[1]['sales_order_line_item_id'] = 'test';

Produces the error. 产生错误。 Is there a workaround? 有解决方法吗?

Edit: As noted by others, you have a typo, but even After correcting the typo it will not work with the code you have written. 编辑:正如其他人指出的那样,您有错字,但即使在纠正错字后,它也无法与您编写的代码一起使用。

You are trying to access an invalid index and as a result you are getting the error. 您正在尝试访问无效的索引,结果您得到了错误。 Use the push method of the Array to add the elements. 使用Array的push方法添加元素。

var lineItemTracker = new Array();
lineItemsTracker.push(result[i].sales_order_line_item_id);

Explaination: 说明:

1. When you create an array like var lineItemTracker = new Array(); 1.当创建像var lineItemTracker = new Array(); lineItemTracker is created an an array but the length is 0 . lineItemTracker创建一个数组,但长度为0

2. Next, when you write lineItemsTracker[i] it means you are trying to access the element at index i . 2.接下来,当您编写lineItemsTracker[i]这意味着您正在尝试访问索引i处的元素。 Since, the length of the array is zero, It will return undefined as there is no element defined at that index. 由于数组的长度为零,因此它将返回undefined因为在该索引处没有定义任何元素。

3. Next, lineItemsTracker[i]['sales_order_line_item_id'] This lines try to access the property sales_order_line_item_id on an undefined object and then you will get the error. 3.接下来, lineItemsTracker[i]['sales_order_line_item_id']这些行尝试访问undefined对象上的属性sales_order_line_item_id ,然后您将得到错误。

Uncaught TypeError: Cannot read property 'sales_order_line_item_id' of undefined on assign 未捕获的TypeError:在分配时无法读取未定义的属性'sales_order_line_item_id'

You have a typo. 你有错字 It should be var lineItemsTracker = new Array(); 应该是var lineItemsTracker = new Array();

You don't have an s in the definition but you put an s everywhere you reference it 您在定义中没有s,但是在引用它的任何地方都放置了s

暂无
暂无

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

相关问题 我为什么会收到错误:Uncaught TypeError:无法读取未定义的属性“ left” - Why am I getting the error: Uncaught TypeError: Cannot read property 'left' of undefined 为什么会出现Uncaught TypeError:无法读取未定义的属性'displayQuestion'? - Why am i getting Uncaught TypeError: Cannot read property 'displayQuestion' of undefined? 在 Firebase 中,我收到 Uncaught TypeError: Cannot read property 'initializeApp' of undefined,但不确定为什么未定义“firebase”? - In Firebase I am getting an Uncaught TypeError: Cannot read property 'initializeApp' of undefined, but not sure why 'firebase' is not defined? 为什么我得到 Uncaught (in promise) TypeError: Cannot read property 'type' of undefined? - Why am I getting Uncaught (in promise) TypeError: Cannot read property 'type' of undefined? 为什么我收到此错误:未捕获的TypeError:无法读取undefined的属性'john' - Why am I getting this error: Uncaught TypeError: cannot read property 'john' of undefined 为什么我收到此错误:“未捕获的TypeError:无法读取未定义的属性'标题'”? - Why am i getting this error: “Uncaught TypeError: Cannot read property 'Title' of undefined”? 为什么我收到“Select.js:1555 Uncaught TypeError:无法读取未定义的属性‘isSelectOptGroup’” - Why I am getting "Select.js:1555 Uncaught TypeError: Cannot read property 'isSelectOptGroup' of undefined" 为什么我会收到“未捕获的类型错误:无法读取未定义的属性 'body'”? - Why am I getting "Uncaught TypeError: Cannot read property 'body' of undefined"? 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM