axecopfire
axecopfire

Reputation: 652

vue-socket.io Uncaught TypeError: Cannot read property of undefined

I am trying to call data from my Sockets object in a Vue component.

data() {
  return {
    message: []
  }
},
sockets: {
  chatMessage: data => {
    console.log(data); //-> my data from server
    console.log(this); //-> undefined 
    console.log(this.message); //-> Uncaught TypeError
  }
}

I'm successfully emitting and receiving the message event from the server because I'm getting data. But I don't know why I can't get the message prop from data.

Here are the docs from the library. https://github.com/MetinSeylan/Vue-Socket.io

Upvotes: 0

Views: 1344

Answers (1)

axecopfire
axecopfire

Reputation: 652

The answer I found was to be that I was using fat arrow functions and so I wasn't binding this.

All I changed was:

sockets: {
  chatMessage: function(data){
    //-> returns what I needed
  }
}

Upvotes: 1

Related Questions