如何在Firebase changeEmail之后显示ionic2警报

时间:2016-05-03 15:26:22

标签: firebase ionic2

我有以下功能来更改Firebase用户帐户上的电子邮件。我想在完成时显示ionic2警报,无论是成功还是出现错误。从下面的代码中我得到警告显示但是它是空白的。很可能这是Firebase承诺的时间问题,但我不知道如何修复它。

private doChangeEmail(data): void {

var myAlert: {
  title?: string, 
  subtitle?: string
} = {};

this.auth.ref.changeEmail({
  oldEmail: data.oldemail,
  newEmail: data.newemail,
  password: data.password
}, function(error) {
  if (error) {
    switch (error.code) {
      case "INVALID_PASSWORD":
        myAlert.title = 'Invalid Password';
        myAlert.subtitle = 'The specified user account password is incorrect.';
        break;
      case "INVALID_USER":
        myAlert.title = 'Invalid User';
        myAlert.subtitle = 'The specified user account does not exist.';
        break;
      default:
        myAlert.title = 'Error creating user';
        myAlert.subtitle = error;
    }
  } else {
    myAlert.title = 'DONE';
    myAlert.subtitle = 'User email changed successfully!';
  }
});

let alert = Alert.create({
  title: myAlert.title,
  subTitle: myAlert.subtitle,
  buttons: [{
    text: 'OK',
    handler: () => {
    }
  }]
});
this.nav.present(alert);
}

2 个答案:

答案 0 :(得分:0)

put the alert code inside the promise result....

this.auth.ref.changeEmail({
  oldEmail: data.oldemail,
  newEmail: data.newemail,
  password: data.password
}, function(error) {
  if (error){
     // do error stuff..
  } else {
     // do success stuff..
  }
  // show alert here...
})

答案 1 :(得分:0)

我发现Frank van Puffelen的以下评论解决了我的问题:

您在回调函数中使用this,它具有不同的含义。一种解决方案是使用fat-arrow / rocket符号进行回调,这可以确保这是你期望的:

正确的语法应该是

this.auth.ref.changeEmail({
  oldEmail: data.oldemail,
  newEmail: data.newemail,
  password: data.password
}, (error) => {
  if (error){
     // do error stuff..
  } else {
     // do success stuff..
  }
  // show alert here...
})

并且警报显示了Aaron Saunders所指出的相关内容