Team Stats / What am I doing wrong

code:

const team = {
_players: [{
firstName: ‘amin’,
lastName: ‘Harit’,
age: 25,
},
{
firstName: ‘Solomon’,
lastName: ‘Okoronkwo’,
age: 30,
},
{
firstName: ‘Genaro Ivan’,
lastName: ‘Gattuso’,
age: 38,
},
],
_games: [{
opponent: ‘Bochum’,
teamPoints: 5,
opponentPoints: 0
},
{
opponent: ‘BXB’,
teamPoints: 4,
opponentPoints: 2
},
{
opponent: ‘Bayern’,
teamPoints: 3,
opponentPoints: 0
}],
get _players() {
return this._players;
},
get _games() {
return this._games;
},
addPlayer(firstName, lastName, age) {
let player = {
firstName: firstName,
lastName: lastName,
age: age,
};
this.players.push(player);
},
addGame(opponentName, teamsPoints, opponentsPoints) {
let game = {
opponentName: opponentName,
teamsPoints: teamsPoints,
opponentsPoints: opponentsPoints,
}
this.game.push(game);
},
};

team.addPlayer(‘Steph’, ‘Curry’, 28);
team.addPlayer(‘Lisa’, ‘Leslie’, 44);
team.addPlayer(‘Bugs’, ‘Bunny’, 28);

team.addGame(‘köln’, 5, 3);
team.addGame(‘wolfsburg’, 2, 0);
team.addGame(‘gladbach’, 3, 1);

error:

/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:45
this.players.push(player);
^

TypeError: Cannot read property ‘push’ of undefined
at Object.addPlayer (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:45:17)
at Object. (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:57:8)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

Read the error message. You haven’t defined something called “players”.

Please format your code next time.

Thanks for the answer, but if I change the ‘players’ to ‘player’ the error will remain the same.

The problem is not that you’re using “players”, it’s that you haven’t defined “players”. Look through your code, and when you’re ready, tell me where you have defined “players”.

1 Like

the correct syntax would be

this._players.push(player);

in the beginning of the project you defined the property:

You will have the same error with the _games property down the line inside the addGame()

Happy Coding!