问题
I have a Node.js app that represents a class room. I'm using Node version 11.11.0. This app has two files: index.js and student.js. The relevant code looks like this:
index.js
const EventEmitter = require('events');
const Student = require('./student');
async function start() {
eventEmitter.on('newListener', (event, listener) => {
console.log(`Added ${event} listener.`);
});
eventEmitter.on('handRaised', (question) => {
console.log(question);
});
for (let i=0; i<10; i++) {
let student = new Student(`Student #${i+1}`);
student.attend();
}
}
start();
student.js
'use strict';
const EventEmitter = require('events');
class Student {
constructor(name) {
this.name = name;
}
attend() {
// simulate a student randomly asking a question within 10 minutes
let minutes = (Math.floor(Math.random() * 10) + 1) * 60000;
setTimeout(function() {
EventEmitter.emit('handRaised', 'What is the 2 + 3?');
}, minutes);
}
}
module.exports = Student;
When I run this, I get an error that says EventEmitter.emit is not a function
. I've tried several variations without any luck. What am I doing wrong?
回答1:
The answer given by @Bhojendra Rauniyar is correct, imho, but is missing a working example which is given below. Note, a subtle, but important change I did to define the callback function for setTimeout()
in student.js
: I am using the arrow function () =>
which binds this
of the student instance to the callback function. This is required to call the instance variable from the callback function. Alternatively function () { ... }.bind(this)
can be used.
index.js
const EventEmitter = require('events');
const Student = require('./student');
const eventEmitter = new EventEmitter();
async function start() {
eventEmitter.on('newListener', (event, listener) => {
console.log(`Added ${event} listener.`);
});
eventEmitter.on('handRaised', (question) => {
console.log(question);
});
for (let i=0; i<10; i++) {
let student = new Student(`Student #${i+1}`, eventEmitter);
student.attend();
}
}
start();
student.js
'use strict';
class Student {
constructor(name, eventEmitter) {
this.name = name;
this.eventEmitter = eventEmitter;
}
attend() {
// simulate a student randomly asking a question within 10 minutes
let minutes = (Math.floor(Math.random() * 10) + 1) * 60000;
setTimeout(() => {
this.eventEmitter.emit('handRaised', 'What is the 2 + 3?');
}, minutes);
}
}
module.exports = Student;
回答2:
You can't use emit
on EventEmitter
directly. You need to have an instance of it. For eg.:
const eventEmitter = new EventEmitter();
// Now, you're okay to go with emit
eventEmitter.emit('handRaised', 'What is the 2 + 3?');
To use the same instance, define it in a file and require wherever you'll need it. Then, you're safe to use on
and emit
on it.
来源:https://stackoverflow.com/questions/55196644/node-js-eventemitter-on-event-is-not-responding-to-emit-actions