Jonathan Aguirre
Jonathan Aguirre

Reputation: 51

Uncaught (in promise): false

I have the following error when using alert messages for my login menu:

Runtime Error Uncaught (in promise): false Stack Error: Uncaught (in promise): false

This is the code:

  public login() {
    this.showLoading()
    this.auth.login(this.Login).subscribe(allowed => {
      if (allowed) {        
        //this.navCtrl.setRoot('Inicio');
        this.usuarioLogueado = this.auth.getUserInfo();
        if(this.usuarioLogueado.tipo == "Administrador"){
          this.navCtrl.setRoot(Administrador);
        }
        console.log("bienvenido",this.usuarioLogueado.usuario,this.usuarioLogueado.tipo);
      } else {
        this.showError("Acceso denegado");
      }
    },
      error => {
        this.showError(error);
      });
  }

  showLoading() {
    this.loading = this.loadingCtrl.create({
      content: 'Por favor espere...',
      dismissOnPageChange: true
    });
    this.loading.present().then(() => this.loading.dismiss());
  }

  showError(text) {
this.loading.dismiss().catch(() => console.log('ERROR: Control de loading fallo'));
    let alert = this.alertCtrl.create({
      title: 'Fallo',
      subTitle: text,
      buttons: ['OK']
    });
    alert.present(prompt);
  }


}

Upvotes: 3

Views: 4227

Answers (1)

sebaferreras
sebaferreras

Reputation: 44669

I think the error is related to this line of code:

this.loading.present().then(() => this.loading.dismiss());

I'm not sure why you would like to hide the loading as soon as it becomes visible. The proper way to use a loader, would be to show it before making the http request, and hide it when the request has ended. It'd look like this:

// Assuming you already have a property to hold the instance of the loader 
public loading: any;

public login() {
    this.showLoading().then(() => { // Show the loading before making the request

        this.auth.login(this.Login).subscribe(allowed => { // Make the http request

            this.loading.dismiss().then(() => { // Hide the loading

                if (allowed) {

                    // this.navCtrl.setRoot('Inicio');
                    this.usuarioLogueado = this.auth.getUserInfo();

                    if (this.usuarioLogueado.tipo == "Administrador") {
                        this.navCtrl.setRoot(Administrador);
                    }

                    console.log("bienvenido", this.usuarioLogueado.usuario, this.usuarioLogueado.tipo);

                } else {
                    this.showError("Acceso denegado");
                }
            });
        }, error => {
            this.loading.dismiss().then(() => { // Hide the loading
                this.showError(error);
            });
        });
    });

}

showLoading(): Promise<any> { // <- Return the promise
    this.loading = this.loadingCtrl.create({
        content: 'Por favor espere...',
        dismissOnPageChange: true
    });
    return this.loading.present(); // <- Added the return keyword here
}

showError(text) {
    let alert = this.alertCtrl.create({
        title: 'Fallo',
        subTitle: text,
        buttons: ['OK']
    });
    alert.present(prompt);
}

Upvotes: 3

Related Questions