问题描述
我将 Firebase
与 AngularFire
和 ngrx/effects
(所有内容的最新版本)一起使用。
看看下面的效果:
createGroupRequest$ = createEffect(() =>
this.actions$.pipe(
ofType(CreateGroupActions.createGroupRequest),switchMap(action =>
of(this.afs.collection<Group>('groups').add(action.group))
.pipe(
map(() => CreateGroupActions.createGroupSuccess()),// This is fired instantly,NOT when the request is done
catchError(error => of(CreateGroupActions.createGroupFailure(error)))
))
)
);
constructor(
private actions$: Actions,private store: Store<AppState>,private afs: AngularFirestore,) {
}
我的主要问题是我的成功操作是立即触发的,而不是在 add()
请求完成时触发。我很确定这是因为 firebase 的 collection.add()
函数返回的是 promise 而不是 observable。
我尝试用 of()
包装它以使其成为 Observable,但没有帮助。有什么想法吗?
解决方法
我已经解决了。
将 of()
更改为 from()
使其有效。
当我在官方文档中阅读这些运算符时,我注意到 of()
实际上并没有将参数转换为 Observable,而 from()
正是这样做的。