2017-05-17 42 views
1

我使用警報消息我登錄菜單時,有下列錯誤:未捕獲的(在承諾):假

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

這是代碼:

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); 
    } 


} 
+1

請將您的代碼添加到您的問題。鏈接到您的代碼的圖片是不合適的。 – mrogers

+0

原因是該頁面稍後可能不存在,並且具有相同問題的下一個人將無法看到您的代碼。 –

+0

我添加了代碼。 –

回答

2

我認爲錯誤的是與此代碼行相關:

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

我不確定爲什麼要隱藏一旦它變得可見,就加載。使用加載器的正確方法是在發出http請求之前顯示它,並在請求結束時將其隱藏。它看起來像這樣:

// 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); 
} 
+1

是的!!它的作品thx !!! –

+0

很高興聽到:)因爲它解決了問題,請將答案標記爲接受的答案,以便我們可以關閉該問題。 – sebaferreras

相關問題