Just started node.js and having this error while playing with events module.
TypeError: Cannot read property 'on' of undefined
const event = require('events').EventEmitter();
event.on('myevent', (msg)=>{
for(var i = 0; i < 3; i++){
console.log(`Message is: ${msg}`);
}
});
event.emit('myevent', 'All is fine');
To solve your problem you need to instantiate the event obj.
for example:
const events = require('events');
const event = new events.EventEmitter();
now rest code will work!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.