I'm getting an error that getToken is undefined when called as below and after transpiling with gulp-babel. Moving the constructor to bottom of class does not help either. Can anyone advise?
I think it has something to do with the util inheriting, which may be trying to take ES5 code and apply it to an area where ES6 does things very differently?
var events = require('events');
var util = require('util');
class Report {
constructor(private_key, service_email, debug) {
this.private_key = private_key;
this.service_email = service_email;
this.debug = debug || false;
events.EventEmitter.call(this);
this.getToken( (err, token) => {
if (err) throw err;
return this.emit('ready');
});
}
getToken(cb) {
...
}
}
util.inherits(Report, events.EventEmitter);
module.exports = Report;
I'm getting an error that getToken is undefined when called as below and after transpiling with gulp-babel. Moving the constructor to bottom of class does not help either. Can anyone advise?
I think it has something to do with the util inheriting, which may be trying to take ES5 code and apply it to an area where ES6 does things very differently?
var events = require('events');
var util = require('util');
class Report {
constructor(private_key, service_email, debug) {
this.private_key = private_key;
this.service_email = service_email;
this.debug = debug || false;
events.EventEmitter.call(this);
this.getToken( (err, token) => {
if (err) throw err;
return this.emit('ready');
});
}
getToken(cb) {
...
}
}
util.inherits(Report, events.EventEmitter);
module.exports = Report;
Share
Improve this question
edited Sep 18, 2016 at 9:19
Simon H
asked Jul 1, 2015 at 19:49
Simon HSimon H
21.1k14 gold badges81 silver badges142 bronze badges
7
- curious: does putting the constructor physically at the bottom help? – dandavis Commented Jul 1, 2015 at 19:52
- Can you show a plete code? (Verifiable..) – Amit Commented Jul 1, 2015 at 19:54
-
Works fine for me (
[email protected]
). – robertklep Commented Jul 1, 2015 at 19:55 - @dandavis no, thanks for the idea though – Simon H Commented Jul 1, 2015 at 19:57
- 1 People should really read How to Ask. Save the world a lot of time – Amit Commented Jul 1, 2015 at 20:05
1 Answer
Reset to default 10Judging by the call to events.EventEmitter
in your constructor, you probably also using this:
require('util').inherits(Report, events.EventEmitter);
This breaks the Report
class (not sure why, but I can reproduce the problem).
Instead, use ES6-style inheritance:
class Report extends events.EventEmitter {
constructor(private_key, service_email, debug) {
super();
...
}
getToken(cb) { ... }
}