Using emit throw error: Uncaught TypeError: Cannot read property 'apply' of undefined
See original GitHub issueNote: for support questions, please use one of these channels: stackoverflow or slack
For bug reports and feature requests for the Swift client, please open an issue there.
For bug reports and feature requests for the Java client, please open an issue there.
You want to:
- report a bug
- request a feature
Current behaviour
What is actually happening? Using emit throws error: Uncaught TypeError: Cannot read property ‘apply’ of undefined.
Steps to reproduce (if the current behaviour is a bug)
First, import Socket.io-client and use:
import io from 'socket.io-client';
const socket = io('http://localhost:3000');
Second, try emit:
socket.emit(`${this.evPrefix}::sync_upload_settings`);
It throws this error:
index.js:133 Uncaught TypeError: Cannot read property 'apply' of undefined
at Socket.push../node_modules/component-emitter/index.js.Emitter.emit (index.js:133)
at Socket.push../node_modules/socket.io-client/lib/socket.js.Socket.emit (socket.js:138)
at Socket.push../node_modules/socket.io-client/lib/socket.js.Socket.onconnect (socket.js:335)
at Socket.push../node_modules/socket.io-client/lib/socket.js.Socket.onpacket (socket.js:232)
at Manager.<anonymous> (index.js:21)
at Manager.push../node_modules/component-emitter/index.js.Emitter.emit (index.js:133)
at Manager.push../node_modules/socket.io-client/lib/manager.js.Manager.ondecoded (manager.js:345)
at Decoder.<anonymous> (index.js:21)
at Decoder.push../node_modules/component-emitter/index.js.Emitter.emit (index.js:133)
at Decoder.push../node_modules/socket.io-parser/index.js.Decoder.add (index.js:251)
It actually coming from here(source from socket.io.dev.js):
Emitter.prototype.emit = function(event){
this._callbacks = this._callbacks || {};
var args = [].slice.call(arguments, 1)
, callbacks = this._callbacks['$' + event];
if (callbacks) {
callbacks = callbacks.slice(0);
for (var i = 0, len = callbacks.length; i < len; ++i) {
callbacks[i].apply(this, args); // ------> HERE!!!!
}
}
return this;
};
Expected behaviour
What is expected? I want to send event to server, but it doesn’t.
Setup
- OS: macOS 10.13.1
- browser: Chrome 72.0.3626.121
- socket.io version: 2.2.0
- socket.io.client version: 2.2.0
- node version: 11.0.0
Other information (e.g. stacktraces, related issues, suggestions how to fix)
I used Webpack for bundling.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Socket.io Uncaught TypeError: Cannot read property 'apply' of ...
The cause of this error is that you're trying to call .emit() via the socket argument of your custom event handlers, which is...
Read more >Uncaught TypeError: Cannot read property 'apply' of ...
The issue was I was trying to call a function on the parent directly from the child and that doesn't work. You need...
Read more >Resolving the JavaScript Promise Error "TypeError: Cannot ...
TypeError - Cannot read property 'then' of undefined is thrown when the caller is expecting a Promise to be returned and instead receives ......
Read more >Cannot read property 'apply' of undefined" - Forums - Liferay
In the console, persistent errors : "Uncaught TypeError: Cannot read property 'apply' of undefined" In what may be the reason?
Read more >redux.js:653 uncaught typeerror: cannot read properties of ...
The root cause of the error is that the declared variable doesn't have any value, so by default, JavaScript treats all variables as...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I actually was stuck on this, although it may be for a different reason… I had run into this issue trying to create my own form of “authentication” (if you can call it that.) - and I found my issue…although I don’t understand it. see below. So I was returning a listener for the servers response and checking in an if statement to see if the return of that function had the “.connected” attribute… yes I know quite hacky but… my problem came up later when I had tried to return “socket.on(“allgood”)” without the empty anon function… basically returning socket.on without its second argument caused it for me…
not sure this is helpful for anyone as if you use the library correctly it is very well put together and I definitely don’t suggest doing what I did here lol
For Anybody Searching for this error. I had similar problem. 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 :
make sure that
exists in the moment when the event happens