I am storing a user id in the storage and trying to get the stored id when the page is loaded.
Below is the code snippet
constructor(
private nav: NavController,
private auth: AuthService,
public clockservice: ClockService,
private alertCtrl: AlertController,
private loadingCtrl: LoadingController,
public storage: Storage
) {
storage.get('name').then(val => {
this.username = val;
})
storage.get('id').then(val => {
this.userid = val;
console.log(this.userid);
})
}
ngOnInit() {
console.log("inside");
console.log(this.userid);
getStatus();
}
getStatus() {
this.showLoading();
this.clockservice.getinoutstatus(this.userid).subscribe(
result => {
if (result.success) {
// do something here
} else {
this.showError("API Error.");
}
this.loading.dismiss();
},
error => {
this.showError("API Error.");
}
);
}
The issue I face is the user id is not received in the ngOnInit event. The console log shows the ngOnInit is called even before the user id is received in the constructor.
console log:
inside
1
How to make sure that is ngOnInit even is called after all the get values received in the constructor?