I'm having some issues with typescript, I have this code:
private loadTeams = function(){
let token = sessionStorage.getItem('token');
if(token !== undefined && token !== null && token !== ''){
this._userService.getUserByToken(token)
.subscribe(user => {
this._teamService.getTeamsByUser(token, user)
.subscribe(data => {
this.teamList = data.data;
}, error => {
});
}, error => {
});
} else {
sessionStorage.removeItem('token');
}
}
And gives me the error:
ERROR TypeError: "_this._teamService.getTeamsByUser is not a function"
In other articles, people tell to use arrow function but I think it is already an arrow function?
I have tried to use let that = this; and then call that instead of this in the second function but it didn't work.
Another thing that I have seen people doing is to use .bind(this) but I don't know where do I do that.
Can someone explain to me why is it happening and how to solve that?
loadTeams = function(){breaks your code. TryloadTeams() {insteadfunctionresetsthisto a new scope because of how javascript works. It's messy and nasty, but that's how it works. You can either use methods as @yurzui proposed (which I think is the correct thing to do here), or use arrow functions instead of using thefunctionkeyword, like this:private loadTeams = () => {...}loadTeams() {...}andloadTeams = () => {...}and the same error keeps showingloadTeams = function(this){