如何在Firebase changeEmail之后显示ionic2警报(How to display ionic2 Alert after Firebase changeEmail)

编程入门 行业动态 更新时间:2024-10-22 23:44:17
如何在Firebase changeEmail之后显示ionic2警报(How to display ionic2 Alert after Firebase changeEmail)

我有以下功能来更改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

更多推荐

本文发布于:2023-07-28 01:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298275.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   如何在   Firebase   changeEmail   display

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!