I have a problem when I try to call a function with an alert after an asynchronous function.
here is my code :
login() {
let loader = this.loading.create({
content: 'Veillez patienter...',
});
loader.present()
.then(() => {
this.cysService.getUserName(this.connexion.pseudo)
.subscribe(res => {
this.monUser = res;
this.lepseudo = this.monUser[0].username;
this.mdp = this.monUser[0].password;
});
}).then(() => {
loader.dismiss();
this.connecter();
});
};
connecter() {
if (this.connexion.pseudo == this.lepseudo && this.connexion.mdp == this.mdp) {
this.navCtrl.push(TabsPage);
} else {
let alert = this.alertCtrl.create({
title: "Erreur de connexion",
subTitle: "Mot de passe ou pseudo incorrect",
buttons: ["Ok"]
});
alert.present();
}
}
login() is called by my form’s onSubmit.
With this code, I have to login twice because the first time, it doesn’t work, I guess it’s because I call connecter in a synchronous way while the program is still trying to get the user’s data.
A loader is shown while I recover the user’s data that I need to check for the login.
I’ve try to call connecter() after the .subscribe(), it works, but the alert in connecter() won’t show up.
Any ideas of how I could fix this problem ?
Thank you.