Zain Ul Abidin
Zain Ul Abidin

Reputation: 2688

Node.JS Module with custom parameter and EventEmitter not working

Hello i am trying to create a Node.JS based module which will accept a parameter when required will be called and also it will be able to raise events that caller module will be able to listen on my approach is some thing like below

let events = require('events');

exports.Inititalize = function (parameter) 
{
    let emitter = events.EventEmitter();

    //some initialization code here
    //at some point need to raise event e.g.
    //emitter.emit('status', 'Hi there i am an event, you provided ' + parameter)

    return emitter;
}

and the caller script invokes it as below

let myModule = require('./mymodule.js').Inititalize('my parameter');

myModule.on('status', function (status) { //TypeError: Cannot read property 'on' of undefined but as far as i know "on" is the part of EvenEmitter that i returned
    console.log(status);
});

What am i doing wrong?

Upvotes: 0

Views: 269

Answers (1)

vsemozhebuty
vsemozhebuty

Reputation: 13772

It seems you just forgot the new keyword:

let emitter = new events.EventEmitter();

Upvotes: 1

Related Questions