Error Message with Team Stats Project

Team Stats

Hi! There’s an error in the last section of my code:. I’ve been logging to console throughout so it seems to be the last section where I invoke the addPlayer function. I’ve tried this.players.push(player); and this._players.push(player); but it doesn’t make a difference.

const team = {
_players: {
  captain: {
  firstName: 'Pablo',
  lastName: 'Sanchez',
  age: 31
  },
  defender: {
    firstName: 'John',
    lastName: 'Wright',
    age: 29
  },
  offense: {
    firstName: 'Andrew',
    lastName: 'Watkins',
    age: 33
  }
},
_games: {
  game1: {
    opponent: 'Chelsea',
    teamPoints: 4,
    opponentPoints: 2
  },
  game2: {
    opponent: 'Arsenal',
    teamPoints: 3,
    opponentPoints: 1
  },
  game3: {
    opponent: 'Liverpool',
    teamPoints: 5,
    opponentPoints: 6
  }
},
get games() {
  return this._games;
},
addPlayer(firstName, lastName, age) {
  let player = {
    firstName: firstName,
    lastName: lastName,
    age: age
  };
  this.players.push(player);
},
};

team.addPlayer('Steph', 'Curry', 28);
team.addPlayer('Lisa', 'Leslie', 44);
team.addPlayer('Bugs', 'Bunny', 76);

console.log(team._players);

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

TypeError: Cannot read property ‘push’ of undefined

.push() is an array method, so you can’t use it to add to an object.
Here I think you need to use Object.assign() to add the new player to the _players object.

Thank you for that! However, the project instructs to use .push()? Well, in the ‘hint’ it has code for .push()…

Odd, can you link the exercise please?

Should the _players be an array of objects instead of an object of nested objects? That would allow .push()`.

@data2704285749 I’m sorry, I meant to link the exercise. It’s Team Stats

We had covered nested objects and it’s the objects section of the course so I assumed they wanted me to nest the objects.

Take a closer look at the description for Task #2

1 Like

Whoops - yes you did. I need to read more carefully! Looks like you’ve go it :+1:

@selectall @data2704285749 Thanks the both of you! I must pay more attention in future instead of trying to race through. So, I’ve updated that code as follows but it’s still returning an error message:

const team = {
_players = [
  {
  firstName: 'Pablo',
  lastName: 'Sanchez',
  age: 31
  },
{
    firstName: 'John',
    lastName: 'Wright',
    age: 29
  },
   {
    firstName: 'Andrew',
    lastName: 'Watkins',
    age: 33
  }
]
_games = [
 {
    opponent: 'Chelsea',
    teamPoints: 4,
    opponentPoints: 2
  },
   {
    opponent: 'Arsenal',
    teamPoints: 3,
    opponentPoints: 1
  },
   {
    opponent: 'Liverpool',
    teamPoints: 5,
    opponentPoints: 6
  }
]
get games() {
  return this._games;
},
addPlayer(firstName, lastName, age) {
  let player = {
    firstName: firstName,
    lastName: lastName,
    age: age
  };
  this.players.push(player);
},
};

team.addPlayer('Steph', 'Curry', 28);
team.addPlayer('Lisa', 'Leslie', 44);
team.addPlayer('Bugs', 'Bunny', 76);

console.log(team._players);









  

This is the error message:

/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:19
_games = [
^^^^^^
SyntaxError: Unexpected identifier

You’re welcome

Looks like in the refactoring process, the commas were removed from between the properties of the team object for _players and _games

Whenever you get an “Unexpected identifier” you almost always need to look before it to find the issue

@selectall Thank you! I’ve added commas now after the ] but now its saying:

/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:2
_players = [
^^^^^^^^^^^^
SyntaxError: Invalid shorthand property initializer

Should you be using = or :? Take it step by step

@selectall I used = for the array and = to create the teams object. This is what my notes are guiding me to do! Is that wrong?

@vickyrai yes, that’s not correct

const myObject = {
  _array: [],
  _object: {},
  _arrayOfObjects: [{}, {}, {}],
  _arrayOfArrays: [[], [], []],
  _objectHoldingArray: {
    _thearray: []
  }
};
1 Like

Wow, that reference is so helpful! Thank you! So there are no more errors now, thankfully. But the curly braces are showing up in the console. I don’t think that’s supposed to happen?!

If you’re still using this line, then it makes sense:

console.log(team._players);

You’re logging the entire array of objects, so having { and } in your output isn’t surprising

If the steps had you do that, then they are just confirming everything is set up correctly so you can continue. It’s proving that your push is working

@selectall Thank you so much! I’m finding this happening a lot with projects, just being floored with the errors. I haven’t been reading the recommended book thus far and I’m wondering of that has anything to do with it? Because I haven’t covered array’s in objects etc so far in Codecademy.

You’ll get more comfortable dealing with errors as you continue your journey. It’s natural to feel overwhelmed by them in the beginning.

I’m not sure where it’s covered in the material and can’t say whether it’s in that book, specifically. It’s possible that it isn’t explicitly covered because arrays are covered before objects, and objects can hold any type of data. Projects like this help reinforce that it all works together.

1 Like