I encountered an issue while setting a value in the constructor of my page's constructor
class. I made a call to the provider
to fetch data. Within the service
call, I was able to retrieve the data successfully. However, when I tried to access my variables outside of the service call, it displayed as undefined
. What could be the reason for this?
HomePage.ts
export class HomePage {
public data: any;
constructor(private nav: NavController, private auth: Auth, private params: NavParams) {
this.auth.getProfile().then((profile) => {
this.data = profile;
console.log('I can see the correct data here ' + JSON.stringify(this.data));
})
console.log('Data is undefined here ' + JSON.stringify(this.data));
}
auth.ts provider
getProfile(){
return this.getToken().then((token) => {
// console.log(token);
if (token) {
return new Promise(resolve => {
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
try {
this.http.get('http://laraveldev/api/users/get_user?token=' + token).subscribe(data => {
this.profile = data.json().data;
//console.log('getProfile ' + JSON.stringify(this.profile));
resolve(this.profile);
});
} catch (error) {
console.log(error);
this.data = { status: 'error' };
resolve(this.data);
}
});
}
});
}