0

I know this issue is well discussed, but I simply couldn't find a proper smooth solution.

The problem: I simply call a method of a class, but the "this" becomes global instead of the self class.

main code:

async.parallel({
        users1: function(cb){userRepository.all(cb)},
        users2: userRepository.all
    },
    function(err, results) {
        //dontcare
    });

appt-repository code:

var User = require('../models/user');

var UserRepository = function() {
    this.users = {};
};

UserRepository.prototype.all = function(done) {
    console.log(this);
    done(null, this.users);
};

module.exports = new UserRepository();

so as you can see in the main code: there are two calls, one inside a callback that works perfectly, and the second one, that simply places the class method. The second one is the problem...

It is easilly solved with option one, but I want to have a more elegant way then just creating anonymous function just for fixing "thi

Tzook Bar Noy
  • 11,337
  • 14
  • 51
  • 82

0 Answers0