Reputation: 5
I have Node.js EventEmitter error. When i'm running my script i'm getting this error. Script is under backtrace. It's billing object(it must update money value in MongoDB).
C:\node\billing\node_modules\models\ticket.js:20
emitter.on('new_bill', function(){
^
TypeError: Object function EventEmitter() {
this.domain = null;
if (exports.usingDomains) {
// if there is an active domain, then attach to it.
domain = domain || require('domain');
if (domain.active && !(this instanceof domain.Domain)) {
this.domain = domain.active;
}
}
this._events = this._events || {};
this._maxListeners = this._maxListeners || defaultMaxListeners;
} has no method 'on'
at Object.<anonymous> (C:\node\billing\node_modules\models\ticket.js:20:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (C:\node\billing\node_modules\routes\pay.js:1:76)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
DEBUG: Program node app exited with code 8
Here's my script:
var emitter = require('events').EventEmitter;
var log4js = require('log4js');
var log = log4js.getLogger();
var redis = require("redis"),
client = redis.createClient();
var HttpError = require('error').HttpError;
var User = require("models/user").User;
function ticket(req, res, next, code, amount){
this.req = req;
this.res = res;
this.next = next;
this.code = code;
this.amount = amount;
}
emitter.prototype = ticket;
emitter.on('new_bill', function(){
client.set(this.code, this.amount);
});
emitter.on('bill_closed', function(){
client.get(this.code, function (err, amount) {
if(err) var err = new HttpError(500, "Redis connection failed");
log.error(err);
this.next(err);
User.upMoney(this.req.session.user._id, amount, function(err){
if(err) var err = new HttpError(500, "Redis connection failed");
log.error(err);
this.next(err);
});
});
});
exports.module = emitter;
Here, i am using node js with v0.10.25. I didn't find any way to solve this issue in Google. Can anyone help to solve this issue?
Upvotes: 0
Views: 2346
Reputation: 12420
You are not creating your object the right way.
You should use inheritance:
var EventEmitter = require('events').EventEmitter;
var util = require('util');
function Ticket(req, res, next, code, amount){
EventEmitter.call(this);
this.req = req;
this.res = res;
this.next = next;
this.code = code;
this.amount = amount;
}
util.inherits(Ticket, EventEmitter);
var ticket = module.exports = new Ticket();
ticket.on('new_bill', function () { /* ... */ });
ticket.on('bill_closed', function () { /* ... */ });
Upvotes: 0
Reputation: 9359
The EventEmitter
is supposed to be instantiated with new
.
var Emitter = require('events').EventEmitter,
emitter = new Emitter();
http://nodejs.org/api/events.html
Upvotes: 3