0

Actually I'm working on a Ionic 2 application which is connected to my firebase backend. When I try to change the password of the actual user everything works fine, but the problem is that the promise, to change the password, is called twice.

I call the function in my editscreen:

changePassword() {

 if(this.passwordchangeForm.valid) {

  this.auth.changePasswort(this.passwordnew.value, this.password.value).then(data=>{
      let toast = this.toastCtrl.create({
        message: "Passwort erfolgreich geändert",

      })
      toast.present();

  }).catch(error=>{
    let alert = this.alertCtrl.create({
      title: 'Fehler beim ändern des Passworts',
      message: error,
      buttons: ['OK'] 
    })
    alert.present();

  })
 }  
}

Which calls the method changePassword() in my authprovider

changePasswort(passNew, password){
var currUser = this.firebase.auth().currentUser;
    return new Promise((resolve, reject) =>{
        this.loginWithEmail({email: currUser.email, password: password}).subscribe(data =>{
            currUser.updatePassword(passNew).then(result =>{
                resolve("Passwort Updated")
            }).catch(error =>{
                 reject(error)
            })
        }, error=>{
            reject(error);
        })
  })
}

If everything is correct the password is changed and the Toast is presented, if not the error shows up. But the problem is that everything pops up 2 or 3 times.

Someone of you can help me to fix that?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Stevetro
  • 1,933
  • 3
  • 16
  • 29
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Jul 11 '17 at 14:46
  • i red the post you linked, but i dont rly know what i should change. If i write the function without promise its always running over the code and not "waiting" for the result from firebase – Stevetro Jul 11 '17 at 14:55
  • Can you show loginWithEmail & updatePassword code – digit Jul 11 '17 at 15:40
  • @Stevetro You should not call `updatePassword`, which already returns a promise, inside the `Promise` constructor. You might need the `new Promise` to wait for the subscription, but the rest should be chained using `then` – Bergi Jul 11 '17 at 18:40
  • can you give me a example for that? i tried to exclude the Promise out of the function but then the error is throw that no then is useable for a "void" function – Stevetro Jul 11 '17 at 23:47

0 Answers0