babbaggeii
babbaggeii

Reputation: 7737

TypeError: Cannot read property 'push' of undefined

I'm trying to push the 'currentBall' object into the 'currentOver.balls' array

$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?

Upvotes: 0

Views: 11797

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074475

It's hard to tell from your quoted "current state" because I suspect important indentation has been lost, but it looks like currentOver, being an array, contains an entry with a balls property (rather than having a balls property of its own). So:

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

Upvotes: 1

Related Questions