Hello everybody, im having a little bit of an issue but im not sure if its my inexperience or something else hope someone can help me.
I have a login form that sends info to my backend and if everything its successful sends me to the main page of the app.
When the backend responds with error code 200 meaning all was successful, i store data to my local storage to use it along the app usage like Client name.
This is part of my login function:
Im using Angular Http module, to make the post into my backend and then i store it on my local storage.
this.GetService.getData(
LOGINPREM?${urlSearchParams.toString()}
)
.subscribe(async data => {
this.result = JSON.stringify(data); // get data in result variable
this.allData = JSON.parse(this.result); // parse json data and pass json string
this.dataService.setjsonresult(this.result);
// alert(this.dataService.getjsonresult());
this.storage.set(‘ID’, this.allData[‘Cliente’]);
this.storage.set(‘Nombre’, this.allData[‘Nombre’]);
this.storage.set(‘Puntos’, this.allData[‘SaldoPuntos’]);
this.storage.set(‘Auth’, 1 );
this.GetService.id = this.allData[‘Cliente’];
this.GetService.cliente = this.allData[‘Nombre’];
this.GetService.puntos = parseInt(this.allData[‘SaldoPuntos’], 0 );
this.GetService.isOk = 1;
this.authService.login();
if (this.allData[‘Error’]== 200) {
const toast = await this.toastController.create({
message: "Bienvenido " + this.allData[‘Nombre’],
position: ‘top’,
duration: 2000
});
toast.present();
this.router.navigateByUrl(‘/menu’);
return false;
} else {
const toast = await this.toastController.create({
message: "Codigo de Error: " + this.allData[‘Error’] + " Descripcion: " + this.allData[‘Descripcion’] ,
position: ‘top’,
duration: 2000
});
toast.present();
this.router.navigateByUrl(‘login’);
return false;
}
When i get to the main page on the ngOnInit, i use the this.storage.get to get all the variables i need like username for example.
This is my ngOnInit.ts
ngOnInit() {
this.isOk = this.newsService.isOk;
if (this.isOk) {
this.id = this.newsService.id;
this.cliente = this.newsService.cliente;
this.points = this.newsService.puntos;
} else {
this.storage.get(‘ID’).then((id) => {
this.id = id;
const urlSearchParams = new URLSearchParams();
urlSearchParams.append(‘cliente’, id);
this.newsService.getData(puntos?${urlSearchParams.toString()}
).subscribe(data => {
this.data = data;
if (this.data[‘Error’] === 200) {
this.storage.set(‘Puntos’, this.data[‘SaldoPuntos’]);
this.points = parseInt(this.data[‘SaldoPuntos’], 0 );
}
});
});
this.storage.get(‘Nombre’).then((cliente) => {
this.cliente = cliente;
});
}
// this.auth();
}
I tried to store all the values in a middle service just to make sure i have the information available when getting into the main page.
My problem is when its the first time and ONLY in the first time my main page shows up, seems like the info is not ready when im trying to use it (this.cliente variable) appears empty when i display it on my html.
And when im running ionic serve on chrome i check all the information on the local storage and its there already.
How it looks like the first time i get to the main page:
How it SHOULD look like when i get to the main page, and how it looks after refreshing the page or closing and getting the app on again on device:
So i think theres a problem with how im trying to retrieve all the data.
Should i use: this.storage.ready and inside the then retrieve all the data?
i.e
this.storage.ready().then(() => {
/** set / get some data from the local storage */
}).catch((error: Error) => {
console.error(error);
return;
})
Or i’m doing something else wrong?
Thanks for your time to read my problem, have a great day.