[英]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.