Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调用Promise.catch中的函数未定义_Javascript - Fatal编程技术网

Javascript 调用Promise.catch中的函数未定义

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 }, {

试图从Promise.catch中调用函数以处理错误情况,但我不确定如何构造它以避免获得未定义的引用

目标是调用async 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

只需将
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中使用
。您的所有代码可能重复发布,无法知道
在您的示例中引用了什么
,并检查其内部内容。使用
确定问题范围。将其转换为箭头函数