代码之家  ›  专栏  ›  技术社区  ›  Eliya Cohen

Firebase在NGXS操作中使用signInWithEmailAndPassword时忽略catch/then

  •  0
  • Eliya Cohen  · 技术社区  · 7 年前

    我有一个通过电子邮件和密码登录用户的操作:

    export class LoginWithCredentials {
      static readonly type = '[Auth] Login With Credentials';
    }
    

    onSubmitLogin() {
      this.store.dispatch(LoginWithCredentials);
    }
    

    操作处理程序从状态获取电子邮件、密码和调用 firebase.auth().signInWithEmailAndPassword ,例如:

    @Action(LoginWithCredentials)
    loginWithCredentials({getState}: StateContext<AuthStateModel>) {
      const {email, password} = getState().forms.login.model;
    
      this.afAuth.auth.signInWithEmailAndPassword(email, password)
        .catch(err => console.log(err)); // Should console.log on error
    
      console.log('Should print something');
    }
    

    catch 正在被忽略,而 console.log 那应该打印一些东西,也被忽略了。

    我试着在州外运行这个方法,它似乎奏效了。尽管如此,我想把这个逻辑运用到我的行动中。

    如果我使用 signInWithPopup(provider) 而不是 signInWithEmailAndPassword ,然后它就会工作(但这不是我需要的)。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Ostn    7 年前

    如果 signInWithEmailAndPassword 返回承诺尝试以下操作:

    @Action(LoginWithCredentials)
    async loginWithCredentials({getState}: StateContext<AuthStateModel>) {
    
      const {email, password} = getState().forms.login.model;
    
      await this.afAuth.auth.signInWithEmailAndPassword(email, password)
        .catch(err => console.log(err)); // Should console.log on error
    
      console.log('Should print something');
    
    }
    

    @Action(LoginWithCredentials)
    async loginWithCredentials({getState, patchState}: StateContext<AuthStateModel>) {
    
      const {email, password} = getState().forms.login.model;
    
      const loginAnswer = await yourLoginService.login(email, password);
    
      if( loginAnswer === 'success' ) {
          patchState( { loggedIn: true };
      }
    
    }
    
    推荐文章