cannot access 'this' in nested function

So I need some help I've been stuck on this for a couple of hours now..

The problem is I'm creating this object called Rank. Rank needs to do some DB calls in mongodb to get data to fill a matrix, then needs to perform even more nested function calls on this matrix which I haven't written yet. The problem is that when I'm calling doRank() its gets the companies and transactions and seems to put them in Rank but then when I call createMatrix it can't access 'this'

I have tried using .bind(this) on each of the functions in doRank() which prevents the error that the method doesn't exist but then when I do the console.log at the end of doRank() the results for matrix are undefined.

function Rank(dampingfactor){
  this.damping_factor = dampingfactor;
  this.companies = null;
  this.transactions = null;
  this.matrix = null;

  this.doRank(function(){
    //companies returns valid
    console.log(this.companies);
    //transactions return valid
    console.log(this.transactions);
    //matrix returns as undefined... help here please
    console.log(this.matrix);
  });
};

Rank.prototype.doRank = function(callback) {
  this.getTransactions(function() {
    this.getCompanies(function () {
        this.createMatrix(function(){
            callback();
        }.bind(this));
    }.bind(this));
  }.bind(this));
};

Rank.prototype.getTransactions = function(callback){
  Transaction.find({}, function(err, transactions) {
    //blah blah blah
    this.transaction = transactions;
    callback();
  });
};

Rank.prototype.getCompanies = function(callback){
  Company.find({}, function(err, comps) {
    //blah blah blah
    this.transaction = comps;
    callback();
  });
};

Rank.prototype.createMatrix = function(callback){
  console.log(this.companies)
  //this returns "null"
  var matrix = new Array(_.size(this.companies));
  for(var i = 0; i < _.size(this.companies); i++){
      matrix[i] = new Array(_.size(this.companies));
      for(var j=0; j < _.size(this.companies); j++){
        matrix[i][j] = 0;
      }
  }
  this.matrix = matrix;
  callback();
};

You are losing context in AJAX callback functions inside getTransactions and getCompanies too. Try this:

Rank.prototype.getTransactions = function(callback) {
    Transaction.find({}, function(err, transactions) {
        //blah blah blah
        this.transaction = transactions;
        callback();
    }.bind(this));
};

Rank.prototype.getCompanies = function(callback) {
    Company.find({}, function(err, comps) {
        //blah blah blah
        this.transaction = comps;
        callback();
    }.bind(this));
};

this no longer refers to an object when used inside nested function calls, as the function does not have a parent object. If you assign this to a variable e.g. var self = this, you can access it like normal inside nested function calls.