Javascript 调用Promise.catch中的函数未定义
试图从Promise.catch中调用函数以处理错误情况,但我不确定如何构造它以避免获得未定义的引用 目标是调用async login()函数,如果密码无效,则向用户显示一条消息Javascript 调用Promise.catch中的函数未定义,javascript,Javascript,试图从Promise.catch中调用函数以处理错误情况,但我不确定如何构造它以避免获得未定义的引用 目标是调用async login()函数,如果密码无效,则向用户显示一条消息 // Log in user login(email, password){ //send login request to firebase this.af.auth.login( { email: email, password: password }, {
// 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
只需将
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
}
这可能是我做的,如果我错了,请告诉我。只需将
当前
添加到方法中,并根据需要使用它。这是我每次都做的链接破解
// 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
}
这可能是我在做的,如果我错了,请告诉我。我最近遇到了firebase API的类似问题。我可以通过使用箭头函数而不是catch函数中的普通函数来解决它,比如:
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((error)=>{
current.showLoginErrorWindow(error);
);
}
我最近遇到了firebase API的类似问题。我可以通过使用箭头函数而不是catch函数中的普通函数来解决它,比如:
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((error)=>{
current.showLoginErrorWindow(error);
);
}
Promise是一种javascript异步方法,您正在尝试在javascript中使用
this
。发布所有代码可能存在重复,无法知道this
在您的示例中引用了什么this
,并检查其内部内容。使用this
确定范围的问题。将其转换为箭头函数Promise是一种javascript异步方法,您正试图在javascript中使用此
。您的所有代码可能重复发布,无法知道此
在您的示例中引用了什么此
,并检查其内部内容。使用此
确定问题范围。将其转换为箭头函数