最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Socket.io Uncaught TypeError: Cannot read property 'apply' of undefined - Stack Overflow

programmeradmin3浏览0评论

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!

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!

Share Improve this question edited Apr 6, 2019 at 19:46 Dacre Denny 30.4k5 gold badges51 silver badges66 bronze badges asked Nov 15, 2018 at 2:09 user10654908user10654908 852 silver badges8 bronze badges 1
  • We don't see all your code - please post all the code you are using so we can help fix your problem. – Jack Bashford Commented Nov 15, 2018 at 2:20
Add a ment  | 

2 Answers 2

Reset to default 3

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!

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)
发布评论

评论列表(0)

  1. 暂无评论