Milad
Milad

Reputation: 859

Node.js Cannot read property 'on' of undefined

What is wrong?

p.s. : I am new to node.js and I'm from .Net world!

My server.js code :

    var events = require('events').EventEmitter;
    var v = function() {
        var e = new events();
        e.emit('start');
    };

    var r = v();
    r.on('start', function(){
        console.log('event start just fired!!!!!!!!!!');
    });

and this console output :

TypeError: Cannot read property 'on' of undefined
    at Object.<anonymous> (E:\Project\node\BasicSocial\server.js:12:2)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:442:10)
    at startup (node.js:136:18)
    at node.js:966:3

Upvotes: 7

Views: 26494

Answers (4)

SHIVANSHU
SHIVANSHU

Reputation: 1

You must forget to write new before making the event object

const events=require("events");
const event=***new*** events.EventEmitter();
event.on('click' ,() => console.log("hello"));
event.emit('click');

Upvotes: 0

Ashish Singh
Ashish Singh

Reputation: 479

You can also use Process.nextTick() or setTimeout(yourFunction,0).

These two functions will come handy when you are emitting and using a callback function.

Here, using the nextTick function, I am able to push the function in the event loop which will be executed after all the processes in the call stack are executed i.e., in the end.

var EventEmitter = require('events').EventEmitter;

var getValue = function(val){
    var e = new EventEmitter();
    process.nextTick(function(){
        var c = 0;
        e.emit('Start');
        var t = setInterval(function(){
            e.emit('Processing data', ++c);
            if(c==val){
                e.emit('Stopped',c);
                clearInterval(t);

            }
        },10);

    });
    return e;
}

var r = getValue(7);

r.on('Start', function(){
    console.log("Started");
});

r.on('Processing data', function(d){
    console.log('Data '+d);
});

r.on('Stopped', function(d){
    console.log('Stopped '+d)
});

Upvotes: 1

Marcos Casagrande
Marcos Casagrande

Reputation: 40394

r is undefined, since v isn't returning anything. That's why you're getting the error. And even if you return the event, your code won't give you the desired output, since you need to use on("start") before you use emit("start")

var events = require('events').EventEmitter;

var e = new events();

e.emit('start'); //This won't trigger the console.log

//Need to be binded before you emit the event.
e.on('start', function(){
    console.log('event start just fired!!!!!!!!!!');
});

e.emit('start'); //This will trigger the console.log

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You forgot to return the event emitter from your v function:

var v = function() {
    var e = new events();
    e.emit('start');
    return e;
};

Also notice that the start event will not be called because you have emitted the event before you subscribed to it. So you could kind of rework your code a little:

var events = require('events').EventEmitter;
var v = function() {
    var e = new events();
    return e;
};

var r = v();
r.on('start', function(){
    console.log('event start just fired!!!!!!!!!!');
});

// emit the event after you have subscribed to the start callback
r.emit('start');

Upvotes: 12

Related Questions