Yesterday and today I’ve spent a couple hours debugging an issue with Prometheus ODM. Problem was, while testing by refreshing a page generated by an ODM model, I noticed that events inside current model are fired on all models created prior. Sample log output was like this:
about to emit "fobidden" to model_5 403 forbidden... model_1 403 forbidden... model_2 403 forbidden... model_3 403 forbidden... model_4 403 forbidden... model_5
From this output I realized that all event callbacks are added to the same instance of EventEmitter.
So, what was the error?
The error was that my model constructors in Prometheus inherited
new EventEmitter methods, and they were added to model constructor prototype. Now model constructor’s prototype was shared between all model instances of the same type, and thus EventEmitter instance was same for all models as well.
Solution to this issue was easy: instead of extending model constructor prototype with
new EventEmitter methods, I moved it inside model constructor, and extended instance’s
new EventEmitter methods. It completely solved the problem.
These fixes are reflected in the newly published NPM module firstname.lastname@example.org.