Team Stats


#1

I am receiving an error code for my getter

/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:6
get:function(games){return this._games;}
^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
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)

const  team = {
    _games:[{opponent: "other guy", teamPoints: 42, opponentPoints: 27}],
    _players:[{firstName:"Random",
               lastName:'Guy',
               age:9999999999999999,   }]
  get games(){return this._games;}
  get players(){return this.player;}
addplayer(firstName, lastName, age){
  let player={firstName:firstName,
              lastName:lastName,
              age:age}
  this.players.push(player);
 
}
 
  };

#2

after value of _players property, you miss a comma:

_players:[{firstName:"Random",
               lastName:'Guy',
               age:9999999999999999,   }] // missing ,

when getting such an error, also always look at the line above


#3

Thanks the commas helped. as I proceeded I received another error

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

TypeError: Cannot read property ‘push’ of undefined
at Object.addPlayer (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:26:17)
at Object. (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:38:7)
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)


#4

TypeError: Cannot read property ‘push’ of undefined

so this.players is undefined. How could that be? Maybe insert a console.log to see its value? Where it goes wrong?


#5

console.log
I placed one to no avail


#6

here:

this.players.push(player);

this will trigger the getter method, so maybe the getter method isn’t returning the right value?

console.log() can very handy for debug, see:

get players(){
    // lets peek at the returned value
    console.log(this.player);
    return this.player;
}

#7

thank you, the error messages are now gone!


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.