FrOz FrOz - 2 years ago 168
Node.js Question - listener must be a function on event

I'm trying to rewrite my server in order to dodge the "callback hell" thing. Now I'm trying to listen for an event, and instead of passing an anonymus function as callback, I want to pass a function exported by a module.

Basically I want to do this

socket.on('joinChat', myFuncExportedByModule(/* stuff */));

Instead of this

socket.on('joinChat', function() {
/* stuff */

As soon as I tried to invoke myFuncExportedByModule, node sent me back an "TypeError: listener must be a function".

Any ideas?

Answer Source

I think the error is you're not passing a function, but executing it.

Try remove parenthesis:

socket.on('joinChat', myFuncExportedByModule);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download