如何在 Firebase 更改电子邮件后显示 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);
}
将警报代码放入承诺结果中....
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...
})
我发现 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 所指出的那样
我有以下功能可以更改 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);
}
将警报代码放入承诺结果中....
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...
})
我发现 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 所指出的那样