Reputation: 2656
This class will display this = undefined
. How to pass the context in?
class MeteorAccount implements IService{
constructor() {
Tracker.autorun(function () {
//observe
Meteor.userId());
console.log(this);//undefined
});
}
}
Upvotes: 1
Views: 225
Reputation: 164467
You have two options:
class MeteorAccount implements IService{
constructor() {
Tracker.autorun(function() {
//observe
Meteor.userId());
console.log(this);//undefined
}.bind(this));
}
}
Or the arrow function:
class MeteorAccount implements IService{
constructor() {
Tracker.autorun(() => {
//observe
Meteor.userId());
console.log(this);//undefined
});
}
}
There's another option, I just don't like it, but it's how people used to do it before the arrow function (not sure why not the bind option):
class MeteorAccount implements IService{
constructor() {
var self = this;
Tracker.autorun(function() {
//observe
Meteor.userId());
console.log(self);//undefined
});
}
}
Upvotes: 2