尝试从Promise.catch中调用函数来处理错误情况,但我不确定如何构造它以避免获取未定义的引用
目标是调用异步login()函数,如果密码无效,则向用户显示消息
// Log in user
login(email, password){
//send login request to firebase
this.af.auth.login(
{
email: email,
password: password
},
{
provider: AuthProviders.Password,
method: AuthMethods.Password,
}
).then(function(){
console.log('Success');
})
.catch(function(error){
this.showLoginErrorWindow(error);
);
}
// Display error message to user
// ** This function never gets called **
showLoginErrorWindow(message){
console.log('message: ' + message);
this.loginErrorMessage = 'Invalid email or password';
this.showLoginError = true; //Angular
}
给我错误:
TypeError: Cannot read property 'showLoginErrorWindow' of null
答案 0 :(得分:1)
只需将current
添加到方法中,然后根据需要使用它。这就是我每次都做的链接黑客攻击。
// Log in user
login(email, password){
//send login request to firebase
var current = this;
this.af.auth.login(
{
email: email,
password: password
},
{
provider: AuthProviders.Password,
method: AuthMethods.Password,
}
).then(function(){
console.log('Success');
})
.catch(function(error){
current.showLoginErrorWindow(error);
);
}
// Display error message to user
// ** This function never gets called **
showLoginErrorWindow(message){
console.log('message: ' + message);
this.loginErrorMessage = 'Invalid email or password';
this.showLoginError = true; //Angular
}
这可能是我这样做的,如果我错了请告诉我。