我有以下功能来更改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); }I have the following function to change the email on a Firebase user account. I want to display an ionic2 alert when complete, whether it was successful or there was an error. From my code below I do get the alert to display BUT it is blank. Most likely it is a timing issue on the Firebase promise but I don't know how to fix it.
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); }最满意答案
将警报代码放在承诺结果中....
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... })I found the following comment by Frank van Puffelen which solved my issue:
You're using this inside a callback function, where it has a different meaning. One solution is to use fat-arrow/rocket notation for the callback, which ensures this is what you expect it to be:
The correct syntax should be
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... })and the alert shows correclty as pointed out by Aaron Saunders
更多推荐
发布评论