0

I would like to show alert dialog if login not success, but it not works. Do I do something wrong in my codes.

login(params) {
    this.presentLoader();

    return this.http
        .post(`${this.base_url}/login`, new HttpParams({
            fromObject: {
                'username': params.username,
                'password': params.password,
            }
        }))
        .subscribe((response: any) => {
                this.loader.dismiss();
                this.storage.set('access_token', response.access_token);
                this.storage.set('user_id', response.id);
                this.storage.set('username', params.username);
                this.storage.set('email', response.email);
                this.authState.next(true);
                //other codes

            },
            (error) => {
                this.presentError('Uh oh, Login failed!. Please try again');
            },
            () => {
                this.loader.dismiss();
            }
        );
}

private async presentLoader() {
    this.loader = await this.loadingCtrl.create({
        message: 'pls wait',
    });
    await this.loader.present();
}

private async presentError(message) {
    const alert = await this.alertCtrl.create({
        message: message,
        buttons: ['OK']
    });
    await alert.present();
}

i think its not read everything in the error block, but if i put alert dialog under loader.dismiss it works

uminder
  • 23,831
  • 5
  • 37
  • 72
nurr
  • 1
  • 5
  • Check "this" scope inside presentError message. Also, your error fallback will only be called inside subscribe if your HTTP response code differs from 200, being any of 400 or 500 HTTP codes. – Diego Victor de Jesus Dec 20 '19 at 03:34
  • @DiegoVictordeJesus i've tried to insert error in subscribe but still not working. – nurr Dec 20 '19 at 03:41
  • Inside the complete callback (the third subscribe function) you could check whether an error was thrown or not and try showing your dialog inside it. – Diego Victor de Jesus Dec 20 '19 at 04:06
  • My bad, you cannot check inside onCompleted callback if you got an error. See: https://stackoverflow.com/questions/42104629/angular-2-checking-for-server-errors-from-subscribe – Diego Victor de Jesus Dec 20 '19 at 04:07
  • @nurr did you tried to add await before the call to async func? await this.presentError('Uh oh, Login failed!. Please try again'); – Oded BD Dec 20 '19 at 04:55
  • @OdedBD how can i do that? can you show me how. thanks – nurr Dec 20 '19 at 05:00
  • When you call presentError in the error section try to add await, and I would have console.error in the error section as well to make sure your code did get to there – Oded BD Dec 20 '19 at 05:02
  • (error) => { **await** this.presentError('Uh oh, Login failed!. Please try again'); }, hope it will work.. :) – Oded BD Dec 20 '19 at 05:03
  • @OdedBD i have tried it but its not working. btw, thanks for helping me. if you have another way, pls let me know. – nurr Dec 20 '19 at 06:22
  • @nurr gladly, let me know if this solution solved it so I will post it as an answer as well – Oded BD Dec 20 '19 at 07:16
  • Are you getting an error in the first place? You can check this by logging inside your error block. – Tomas Vancoillie Dec 20 '19 at 07:30
  • i'm not getting any error. even i put **console.log(error)** , it seems dont read anything in my error block. @TomasVancoillie – nurr Dec 20 '19 at 08:04
  • Do you send an error back from your API? Does this `${this.base_url}/login` return an error with a HTTP response code of 500 for example? – Tomas Vancoillie Dec 20 '19 at 08:14
  • If you can't send an error back, you can test your error block by changing the `correct` route to status code 500 and see if it comes back to the error block. – Tomas Vancoillie Dec 20 '19 at 08:15
  • can you teach me how to change to status code 500? i'm still new about this. thank you. @TomasVancoillie – nurr Dec 20 '19 at 08:29

0 Answers0