【发布时间】:2021-08-13 22:44:49
【问题描述】:
实际目标:
- 用户可以在组织组中发帖,在该组织组中发布的帖子不能在其他组织组中可见,因此我尝试使用 Firebase 中的 WHERE 功能来过滤帖子列表。
- 用户发布的每个帖子都会在 Firestore 中保存组织组 ID(创建帖子的位置)。因此,当我过滤帖子列表时,我可以使用组织组 ID(postOrgId) 来过滤每个组织组中发布的特定帖子。
我想我在获取用户使用已激活路由查看的组织 ID 时遇到了问题。
图 1:组织 ID
我尝试以下方法。
-
我尝试使用激活路由,因为它会保存用户查看的当前组织组 ID,但不幸的是 Firebase 抛出错误。
-
我创建了一个获取当前组织 ID 的方法。
图 2:Firebase 抛出错误
这是组织组 (org-home.ts)
export class OrgHomePage implements OnInit, OnDestroy {
loadOrganization: Organization;
orgId: any;
orgSub: Subscription;
posts: Post[];
isLoading = false;
constructor(private orgService: OrganizationService,
private afs: AngularFirestore,
private activateRoute: ActivatedRoute,
private authService: AuthService,
public auth: AuthService,
private storage: AngularFireStorage,
private postService: PostService) {}//
ngOnInit() {
this.isLoading = true;
//This will read the Organization Group ID
this.orgId = this.activateRoute.snapshot.params['orgID'];
// I call the method and passed the Org ID
this.postService.getOrgId(this.orgId);
this.orgSub = this.postService.getPosts().subscribe(posts => {
this.posts = posts;
this.isLoading = false;
});
}
以下是邮政服务 (post.service.ts)
export class PostService {
postCol: AngularFirestoreCollection<Post>;
postDoc: AngularFirestoreDocument<Post>;
posts: Observable<Post[]>;
post: Observable<Post>;
post$: any;
orgId: Observable<any>;
constructor(
private afs: AngularFirestore
) {
this.postCol = this.afs.collection('post', ref => ref.orderBy("createdAt", "desc"));
//Organization Group ID will be passed here to the WHERE Clause
this.postCol = this.afs.collection('post', ref => ref.where("postOrgId", "==", this.orgId));
this.posts = this.postCol.snapshotChanges().pipe(
map(action => {
return action.map(a => {
const data = a.payload.doc.data() as Post;
data.postId = a.payload.doc.id;
return data;
})
})
);
}//
//Gets all post
getPosts() {
return this.posts;
}
//It will get the Organization ID
getOrgId(idParameter): Observable<any>{
return this.orgId = this.afs.collection('post', result => result.where('postOrgId', '==', idParameter)).valueChanges();
}
【问题讨论】:
-
你能确定
this.orgID已定义吗/ -
在我的帖子服务中,this.orgID 没有被读取,所以我想我在获取用户查看的当前组织 ID 时遇到了问题。
标签: angular typescript firebase ionic-framework google-cloud-firestore