i have the folloing code:
export class AuthService {
token: string;
refreshToken: string;
constructor(private router: Router) { }
signinUser(username: string, password: string) {
const user = new mylib.User(username, password);
user.requestSessionToken(true, {
onOk: function (result) {
let tokenMap: any;
tokenMap = result;
this.token = tokenMap.sessionToken;
this.refreshToken = tokenMap.refreshToken;
mylib.Datastore.configureWithSessionToken(this.token);
this.router.navigate(['/signup']);
},
onError: function (error) {
console.error(error);
}
});
console.log(this.token);
console.log('signinUser Called!');
}
}
mylib is Javascript library and this is not pointing to my object so this.token is not woking
how can i access my class fields in Javascript block?
user.token.thisrefers to the functionsigninUsersince you are calling it outside of the object literal. Also, it appears thatuser.requestSessionTokenis async, so you may need to use a callback or promise to wait until the function is finished before accessingthis.token.tokenis local member inAuthServiceclass not inuser