我正在创建一个简单的论坛。
我想过滤帖子。我在RxJS中使用.pipe和.filter遇到了一些麻烦。
我试图:
api/posts,当与http.get一起使用时,它返回一个Observable<Post[]>Post中的Observable<Post[]>每个帖子并对其进行过滤,以便仅选择ID为1(每个ID的帖子1)的每个帖子。在filter不选择在每个单独的片Post[]阵列,而是选择Post[]本身。
我的代码:
getPosts(): Observable<Post[]> {
// Sets the url to the service's postsUrl
const url = this.postsUrl;
// The http.get returns an Observable<Post[]>
return this.http.get<Post[]>(url)
.pipe(
filter(
post: Post => post.id == 1
),
// Log posts were fetched
tap(posts => console.log('Posts fetched.')),
// Error handling
catchError(this.handleError('getPosts', []))
);
}
Run Code Online (Sandbox Code Playgroud)
我的错误:
Property 'id' does not exist on …Run Code Online (Sandbox Code Playgroud)