Error with .push in "Team Stats" exercise

Hi everybody! I was trying to do the “Team Stats” exercise in the Javascript Objects lesson (here’s the link https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-objects/projects/team-stats). Even tho I checked it step by step with the help video, I get this error:

/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:48
this.players.push(player);
^
TypeError: Cannot read property ‘push’ of undefined
at Object.addPlayer (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:48:17)
at Object. (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:59:6)
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)

The same thing happened with the exercise “Meal Maker”, and I don’t understand what is wrong with my code. Can someone help me? I paste down here my code for “Team Stats”:

const team = {
  _players: [
    {
    firstName: 'Pablo',
    lastName: 'Sanchez',
    age: 11
    },
    {
    firstName: 'Kevin',
    lastName: 'Durt',
    age: 15
    },
    {
    firstName: 'Daniel',
    lastName: 'Smith',
    age: 16 
    }
  ],
  _games: [
   {
  opponent: 'Broncos',
  teamPoints: 42,
  opponentPoints: 27
   },
   {
  opponent: 'Eagles',
  teamPoints: 50,
  opponentPoints: 60
   },
   {
  opponent: 'Snakes',
  teamPoints: 20,
  opponentPoints: 45
   },  
  ],
  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(opponent, teamPoints, opponentPoints){
    let game = {
      opponent: opponent,
      teamPoints: teamPoints,
      opponentPoints: opponentPoints
    };
    this.games.push(game);
  }
};
team.addPlayer('Steph', 'Curry', 28);
team.addPlayer('Lisa', 'Leslie', 44);
team.addPlayer('Bugs', 'Bunny', 76);

console.log(team.players);

team.addGame('Vipers', 70, 20);
team.addGame('Foxes', 55, 40);
team.addGame('Badgers', 30, 50);

Thanks a lot for your help!

players does not exist. It should be _players

Thanks a lot for your reply! I fixed the code, but now it gives me "Maximum call stack size exceeded error :scream:

/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:37
return this._players;
^

RangeError: Maximum call stack size exceeded
at Object.get _players [as _players] (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:37:16)
at Object.get _players [as _players] (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:37:16)
at Object.get _players [as _players] (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:37:16)
at Object.get _players [as _players] (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:37:16)
at Object.get _players [as _players] (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:37:16)
at Object.get _players [as _players] (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:37:16)
at Object.get _players [as _players] (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:37:16)
at Object.get _players [as _players] (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:37:16)
at Object.get _players [as _players] (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:37:16)
at Object.get _players [as _players] (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:37:16)

1 Like

The above line is a circular reference that results in an infinite loop.

We use a non-prefixed name for the get and set methods…

get players () {
    return this._players;
}
set players (player) {
    this._players.push(player);
}
1 Like