Exploring Javascript ES6 classes with Mongoose and having trouble in accessing class variables. I want to use this.name
inside cursor.on(data)
event referencing the variable declared in the constructor of the class. How can I achieve this??
'use strict';
const Mongo = require('../mongo')
class Example {
constructor() {
this.name = 'Test Class';
}
export(docId, callback) {
console.log('In export' + docId);
const cursor = Mongo.findDocById(docId);
console.log(this.name); // Prints "Test Class"
cursor.on('data', function (document) {
console.log(document);
console.log(this.name); // Prints "undefined"
});
cursor.on('close', function () {
Mongo.close();
callback(null, 'Success')
});
}
}