使用 Ngrx 触发效果时无限循环
Infinite loop when triggering effect with Ngrx
我是使用 Rxjs 和 Ngrx 的小新手,我遇到一个问题,每当发生身份验证错误时 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))
)
为了解决这个问题,我将 dispatch 参数传递为 false。
@Effect({ dispatch: false })
authError$: Observable<Action> = this._actions.pipe(
ofType(authActions.AUTH_ERROR),
tap(action => alert(action.payload.error.message))
)
我是使用 Rxjs 和 Ngrx 的小新手,我遇到一个问题,每当发生身份验证错误时 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))
)
为了解决这个问题,我将 dispatch 参数传递为 false。
@Effect({ dispatch: false })
authError$: Observable<Action> = this._actions.pipe(
ofType(authActions.AUTH_ERROR),
tap(action => alert(action.payload.error.message))
)