使用 Ngrx 触发效果时的无限循环



我对使用 RxjsNgrx 有点新手,我有一个问题,每当发生身份验证错误时,都会显示alert(),并显示从 Firebase 返回的错误消息。

问题是当错误发生时,我只想显示一条带有错误的消息......它进入无限循环,内存使用量增加,我被迫关闭应用程序。

当我正确输入电子邮件和密码时,应用程序的行为方式正确...每当返回 Firebase 错误时,就会出现问题。

如果我不为AUTH_ERROR创建Effect(),则不会发生无限循环......并且我在查看Redux DevTools的应用程序状态中收到错误。

@Effect()
    loginWithEmailPassword: Observable<Action> = this._actions.pipe(
        ofType(authActions.LOGIN_EMAIL_PASSWORD),
        map((action: authActions.LoginEmailPassword) => action.payload),
        switchMap(data => from(this._angularFireAuth.auth
            .signInWithEmailAndPassword(data.email, data.password)).pipe(
                switchMap(response => forkJoin([
                    from(response.user.getIdTokenResult(true)),
                    of(response.user)
                ])),
                map(([token, user]) => {
                    if (user) {
                        this._router.navigate(['']);
                        return new authActions.Authenticated({
                            admin: token.claims.admin,
                            profissional: token.claims.profissional,
                            assinante: token.claims.assinante,
                            firestoreCollection: token.claims.firestoreCollection,
                            user: user
                        });
                    }
                }),
                catchError((error) => of(new authActions.AuthError({ error: error })))
            ))
    )
    @Effect()
    authError$: Observable<Action> = this._actions.pipe(
        ofType(authActions.AUTH_ERROR),
        tap(action => alert(action.payload.error.message))
    )

为了解决这个问题,我将调度参数作为 false 传递。

 @Effect({ dispatch: false })
    authError$: Observable<Action> = this._actions.pipe(
        ofType(authActions.AUTH_ERROR),
        tap(action => alert(action.payload.error.message))
    )

最新更新