Reputation: 1383
I created an event in node.js but when I try to trigger the event and pass arguments to the callback I get eventEmitter("dataIn", null, message); TypeError: object is not a function
. Looking at similar stack overflow questions the problem was not using new
to create the eventEmitter, but I am.
First I setup the event.
var events = require('events').EventEmitter;
var eventEmitter = new events();
Then I set the callback.
function dataIn(config, callback) {
if(typeof callback === 'function') {
/* Set the callback for the dataIn event. */
eventEmitter.on("dataIn", callback);
/* More code below. */
Finally when I try to emit the event I get the type error.
var message = {
"type": "REQUEST"
}
var rtrn = eventEmitter("dataIn", null, message);
if(rtrn === false) {
console.log('Error triggering event');
}
Upvotes: 2
Views: 7656
Reputation: 414036
The EventEmitter instance isn't a function; you're trying to call it as if it were. You need to call the .emit()
method:
eventEmitter.emit("dataIn", null, message);
Upvotes: 3