user1692342
user1692342

Reputation: 5237

Type Error Cannot Read Property Javascript function

I have a function which looks like this:

function instance(options) {
   this.createInstance = Bluebird.method(function() {
               this.getInstance();
    });

   this.getInstance = Bluebird.method(function () {
   });
}
module.exports = instance

Bluebird is a npm library for promises

In my test file using mocha, I import this instance file and create the object by var Instance = new instance(options); and I call Instance.createInstance However I get an error saying TypeError: Cannot read property 'getInstance' of null

Upvotes: 0

Views: 868

Answers (1)

mz3
mz3

Reputation: 1344

The problem is that this isn't available inside Bluebird.method. Create a new reference to this to fix the problem.

function instance(options) {
   var that = this;
   this.createInstance = Bluebird.method(function() {
               that.getInstance();
    });

   this.getInstance = Bluebird.method(function () {
   });
}
module.exports = instance

Upvotes: 1

Related Questions