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._pile (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
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._pile (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
Share
Improve this question
edited Sep 19, 2019 at 17:00
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Jan 31, 2016 at 17:14
MiladMilad
8591 gold badge12 silver badges33 bronze badges
4 Answers
Reset to default 12You 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');
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
You can also use Process.nextTick()
or setTimeout(yourFunction,0)
.
These two functions will e 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)
});
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');