FrOz FrOz - 1 year ago 97
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);