Skip to content

socket.io-client redefines this inside function, can't call any other function or variable #3920

Answered by darrachequesne
krodh asked this question in Q&A
Discussion options

You must be logged in to vote

Did you try with an arrow function:

socket.on("message", (msg) => {
  this.messages.push(msg); // This line
  console.log("Received message");
});

If I'm not mistaken, in your code example above, the this points towards the socket object, hence the Cannot read property 'push' of undefined.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@krodh
Comment options

Answer selected by krodh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants