A particular event doesn’t work in my program where I am using socket.io. The rest works fine. This is where the problem occurs:
First html file:
socket.on('connect', () => {socket.emit('phone', 'phone');});
Server file:
io.on('connection', function(socket){
io.on('phone', function(socket, data){
io.emit('stuurbedrijfsnaam', 'stuurbedrijfsnaam');
});
});
2nd html file:
socket.on('stuurbedrijfsnaam', function(socket){
socket.emit('stuurbedrijfsnaam',bedrijfsnaam)
})
This is the full error given in the console:
index.js:83 Uncaught TypeError: Cannot read property 'apply' of undefined
at r.emit (index.js:83)
at r.onevent (index.js:83)
at r.onpacket (index.js:83)
at r.<anonymous> (index.js:83)
at r.emit (index.js:83)
at r.ondecoded (index.js:83)
at a.<anonymous> (index.js:83)
at a.r.emit (index.js:83)
at a.add (index.js:83)
at r.ondata (index.js:83)
at r.<anonymous> (index.js:83)
at r.emit (index.js:83)
at r.onPacket (index.js:83)
at r.<anonymous> (index.js:83)
at r.emit (index.js:83)
at r.onPacket (index.js:83)
at r.onData (index.js:83)
at WebSocket.ws.onmessage (index.js:83)
It references index.js:83, which is inside a folder made by socket.io itself. There are lines 81, 82 and 83:
Backoff.prototype.setJitter = function(jitter){
this.jitter = jitter;
};
Hope I gave enough resources. It would be cool if someone’s help me out. Thanks!
I was getting the same error in the console on every hot-reload:
Check your client import statement for
socket.io-client
should be
Wasn’t sure how you were importing socket.io based on your html sample, but this is what the socket.io docs say.
For Anybody Searching for this error. It can be caused by providing variable as a second parameter in
.on
method that is undefined. Internal code of socket.io tries to call.apply
method to pass arguments into your event handler. If function registered is not existing and thereforeundefined
, this error occurs since undefined values have no such property . It often may be the case of the typo in the name of the functioni.e
should be :
The cause of this error is that you’re trying to call
.emit()
via thesocket
argument of your custom event handlers, which is incorrect usage according to the documentation for socket.io client side API.Consider revising your client side code as follows by removing the
socket
argument from your handlers to cause.emit()
to be called on the actual socket instance:The reason
socket.emit('phone', 'phone');
in your first html file is thatemit()
is called on the original socket instance, rather than via asocket
argument passed to the event handler, as you are doing in your seconds html file.Hope that helps!