我试图在ngrx效果中进行两个http调用。说我有要提取的评论。它首先拨打电话以获取最新评论,并检查它是否已出现在UI上;否则,我再次拨打电话并获取所有评论。这在连续轮询中运行。这是我的代码。我现在正在学习角度性和可观察性是一种混乱。
@Effect()
fetchAllComments: Observable<any> = this.actions$.pipe(
ofType(commentsActions.FETCH_ALL_COMMENTS),
withLatestFrom(this._store),
mergeMap(([action, state]) => {
return this._http.get(`${this.baseUrl}/comments/latest`)
.pipe(
catchError(error => {
console.log(error);
return this._error.handle(error, new commentsActions.FetchAllCommentsError)
}))
.subscribe(comment => {
console.log("Response from server", comment);
if (!!comment['id'] && state.comment.comments.every(n => n.id !== comment['id'])) {
console.log("making call to fetch api ");
return this._http.get(`${this.baseUrl}/comment`)
.pipe(
map((comments: any) => new commentsActions.FetchAllCommentsComplete(comments.message)),
catchError(error => {
return this._error.handle(error, new commentsActions.FetchAllCommentsError);
}));
} else {
console.log("returning mock");
return of(state.comment.comments);
}
})
}));
这是正确的方法吗?我从core.js:15724 ERROR TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
谢谢
答案 0 :(得分:0)
您要从mergeMap返回订阅,您需要返回可观察对象,但是您已订阅它,而订阅就是您要返回的内容。