javascript 类型错误:无法读取未定义的属性“推送”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23131234/
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
TypeError: Cannot read property 'push' of undefined
提问by babbaggeii
I'm trying to push the 'currentBall' object into the 'currentOver.balls' array
我正在尝试将“currentBall”对象推送到“currentOver.balls”数组中
$scope.currentOver.balls.push($scope.currentBall);
The current state of both objects just before the above code is run (from the dev tools):
运行上述代码之前两个对象的当前状态(来自开发工具):
$scope.currentOver: Array[1]
0: Object
balls: Array[0]
bowlerId: 0
byes: 0
legByes: 0
noBalls: 0
runs: 0
wickets: 0
wides: 0
__proto__: Object
length: 1
overId: 1
__proto__: Array[0]
$scope.currentBall: Object
ballId: 0
batsmanId: 0
bowlerId: 0
byes: 0
legByes: 0
noBalls: 0
runs: 2
wicketsNumber: 0
wicketsType: 0
wides: 0
__proto__: Object
I get an error: Cannot read property 'push' of undefined
. But it's clear that the '$scope.currentOver.balls' array is defined, so what's going on here?
我收到一个错误:Cannot read property 'push' of undefined
。但是很明显'$scope.currentOver.balls' 数组被定义了,那么这里发生了什么?
回答by T.J. Crowder
It's hard to tell from your quoted "current state" because I suspect important indentation has been lost, but it lookslike currentOver
, being an array, containsan entry with a balls
property (rather than having a balls
property of its own). So:
很难从您引用的“当前状态”中判断出来,因为我怀疑重要的缩进已经丢失,但它看起来像是currentOver
一个数组,包含一个具有balls
属性的条目(而不是拥有balls
自己的属性)。所以:
$scope.currentOver[0].balls.push($scope.currentBall);
The key bits that make me think that are:
让我想到的关键点是:
$scope.currentOver: Array[1] <== Here, we're seeing that it's an array with one entry 0: Object <== This looks like it's about to show us what that entry is balls: Array[0] <== And so I suspect this is in the entry, not the array bowlerId: 0 ...
E.g., I suspect the lost indentation probably looks something like this:
例如,我怀疑丢失的缩进可能看起来像这样:
$scope.currentOver: Array[1] 0: Object balls: Array[0] bowlerId: 0 ...