Reputation: 17
I am currently migrating an existing Angular2+ code using Auth0 authentication. I have to give up the Lock widget to use the universal login.
Here is a sample of my new service using Auth0.js :
public infosUtilisateur: any = undefined;
public RafraichirInfosUtilisateur()
{
let accessToken = localStorage.getItem('access_token');
if(accessToken && !this.infosUtilisateur)
{
this.webAuth.client.userInfo(accessToken, function(err, user)
{
this.infosUtilisateur = user; // Here goes the pain
});
}
}
Whenever my function is called, i get this error :
Cannot set property 'infosUtilisateur' of undefined
I do not understand why i can't access to my service property inside the API call.
What did I do wrong ?
Upvotes: 0
Views: 46
Reputation: 12960
Use the callback function
without function
keyword and use the =>
insead. This preserves the calling context.
if(accessToken && !this.infosUtilisateur)
{
this.webAuth.client.userInfo(accessToken, (err, user) =>
{
this.infosUtilisateur = user; // Here goes the pain
});
}
Upvotes: 1