Socket.io Uncaught TypeError: Cannot read property 'apply' of undefined

柔情痞子 提交于 2020-08-05 07:54:05

问题


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!


回答1:


The cause of this error is that you're trying to call .emit() via the socket 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:

socket.on('stuurbedrijfsnaam', function(){ // remove 'socket' here

   // cause emit() to be called on actual socket instance
   socket.emit('stuurbedrijfsnaam',bedrijfsnaam) 
})

The reason socket.emit('phone', 'phone'); in your first html file is that emit() is called on the original socket instance, rather than via a socket argument passed to the event handler, as you are doing in your seconds html file.

Hope that helps!




回答2:


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 therefore undefined, this error occurs since undefined values have no such property . It often may be the case of the typo in the name of the function

i.e

socket.on('next_player', this.nextplayer)

should be :

socket.on('next_player', this.next_player)


来源:https://stackoverflow.com/questions/53311432/socket-io-uncaught-typeerror-cannot-read-property-apply-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!