![](/img/trans.png)
[英]Why am I getting the error: Uncaught TypeError: Cannot read property 'left' of undefined
[英]why am I getting Uncaught TypeError: Cannot read property '0' of undefined on assign?
我有:
var lineItemTracker = new Array();
lineItemsTracker[i]['sales_order_line_item_id'] = result[i].sales_order_line_item_id;
這引發錯誤
Uncaught TypeError: Cannot read property '0' of undefined on assign
如果我檢查
console.log(result[i].sales_order_line_item_id);
然后我得到正確的結果。 當我檢查
console.log('here ' + lineItemsTracker[i]['sales_order_line_item_id']);
我收到錯誤消息。
lineItemsTracker[1]['sales_order_line_item_id'] = 'test';
產生錯誤。 有解決方法嗎?
編輯:正如其他人指出的那樣,您有錯字,但即使在糾正錯字后,它也無法與您編寫的代碼一起使用。
您正在嘗試訪問無效的索引,結果您得到了錯誤。 使用Array的push
方法添加元素。
var lineItemTracker = new Array();
lineItemsTracker.push(result[i].sales_order_line_item_id);
說明:
1.當創建像var lineItemTracker = new Array();
lineItemTracker
創建一個數組,但長度為0
。
2.接下來,當您編寫lineItemsTracker[i]
這意味着您正在嘗試訪問索引i
處的元素。 由於數組的長度為零,因此它將返回undefined
因為在該索引處沒有定義任何元素。
3.接下來, lineItemsTracker[i]['sales_order_line_item_id']
這些行嘗試訪問undefined
對象上的屬性sales_order_line_item_id
,然后您將得到錯誤。
未捕獲的TypeError:在分配時無法讀取未定義的屬性'sales_order_line_item_id'
你有錯字 應該是var lineItemsTracker = new Array();
您在定義中沒有s,但是在引用它的任何地方都放置了s
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.