首页 文章

已调用Angular ngrx Firebase OAuth操作,但效果不是

提问于
浏览
5

当我使用此效果注销时,我的oauth设置存在问题:

@Effect()
  logout:  Observable<Action> = this.actions.ofType(UserActions.LOGOUT)
    .switchMap(() => Observable.of(this.afAuth.auth.signOut()))
    .map(() => new UserActions.GetUser())
    .catch(err => Observable.of(new UserActions.AuthError({error: err.message})));

一切正常, UserActions.GetUser() 正在被召唤 . 现在,如果我尝试使用此效果和此firebase auth函数登录:

@Effect()
  googleLogin: Observable<Action> = this.actions.ofType(UserActions.GOOGLE_LOGIN)
    .switchMap(() => Observable.fromPromise(this.loginWithGoogle()))
    .map(() => new UserActions.GetUser())
    .catch(err => Observable.of(new UserActions.AuthError({error: err.message})));

private loginWithGoogle() {
  return this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
}

新的 UserActions.GetUser() 被调用,我可以在Redux DevTools中看到它,但实际效果没有被调用,我放了很多console.log 's in there to see if i did something wrong in the function, but it isn'来调用 GetUser ,而且我也有 catch ,但是这也没有触发 .

在这种情况下是否存在firebase的问题或者我是愚蠢的?

操作:

Dispatched Actions

获得用户效果:

@Effect()
  getUser: Observable<Action> = this.actions.ofType(UserActions.GET_USER)
    .switchMap(() => {
      console.log('test'); // <-- Not called
      return this.afAuth.authState;
    })
    .map(()=> {
      return new UserActions.Authenticated();
    });

** EDIT 1

我做了一个新的观察:当没有互联网连接,所以firebase从本地加载,它实际上工作,不知何故连接到firebase数据库的问题

3 回答

  • 0

    尝试使用这个:

    @Effect()
    googleLogin: Observable<Action> = this.actions.pipe(
    
            ofType(UserActions.GOOGLE_LOGIN),
            map(action => action.payload), // if there is no payload disregard this
            exhaustMap((auth: any) => Observable.fromPromise(this.loginWithGoogle()
                   .pipe(
                      map(() => new UserActions.GetUser()),
                      catchError(err => Observable.of(new UserActions.AuthError({error: err.message})))
                ))
            )
    
        )
    

    为此,exhaustMap和switchMap之间有一点区别 . 如果你成功的话,请告诉我 .

  • 0

    你能试试这个吗?在我看来这可能会有所帮助 .

    @Effect()
      googleLogin = this.actions.ofType(UserAction.GOOGLE_LOGIN).pipe(
        map(() => Observable.fromPromise(this.loginWithGoogle()),
        mergeMap(()=> return[{type: UserActions.GET_USER}])
      );
    
  • 0

    我遇到了类似的问题,我认为效果没有被调用,因为在你的 getUser 效果中你没有实现 catch 块如果效果会抛出任何错误,所以理想情况下在这种情况下如果一个效果出错而且它没有调用't have any catch mechanism the effect won't进一步的行动 .

相关问题