0

I'm making a pollingapp in which I wanted to implement unique nicknames so in the <script> part. I have this data function which contains some parameter which I have been having no trouble calling except for one I have this leaderboard object which is simply: leaderBoard:{nicknames:[], scores:[]},

I have then in the created part made a

socket.on("nickNames", function(nicknames){
  this.leaderBoard.nicknames = nicknames;
}

Or well that's the gist of it my console is then telling me that

"Uncaught TypeError: Cannot set properties of undefined (setting 'nicknames')"

And I have no idea how to fix it and I have checked that nicknames is defined and everything seems to be working okay except for this.

EDIT: apparently when using a function like above this is not actually refering to the data objects the soloutin was to instead use something called a gamma function? which instead of function(nicknames){code} should look like (nicknames) =>{code}

  • 3
    Probably `this` is not what you think it is – Quentin Dec 13 '21 at 17:30
  • but shouldnt this refer to my data objects? this seems to be working correctly in all my ordinary methods ex `methods:{ setNick: function() {socket.emit("setNick",this.nickname)}}` –  Dec 13 '21 at 17:37
  • 1
    `this` will will have whatever value Socket.IO gives it when it gets called. – Quentin Dec 13 '21 at 17:43
  • yes thanks i just realized this, thank you for pointing me in the right direction –  Dec 13 '21 at 17:45

0 Answers0