Current Objective:
- Our main focus here is to allow users to post within their designated Organization Group. These posts should remain exclusively visible within the specific Organization Group they are posted in. To achieve this, I have attempted to implement a filtering functionality using the WHERE clause in Firebase.
- Whenever a user creates a post, the ID of the corresponding Organization Group is saved in Firestore. This Organization Group ID (postOrgId) is then utilized to filter and display only the posts belonging to a particular Organization Group.
However, I seem to be encountering an issue with retrieving the Organization ID that is being viewed by the user through the Activated Route.
https://i.sstatic.net/8iTx0.png
Image 1: Organization ID
In my attempts to solve this problem, I have tried the following approaches:
I initially experimented with using Activated Route as it stores the current Org Group ID viewed by the user. Unfortunately, this implementation resulted in an error being thrown by Firebase.
Subsequently, I created a method specifically designed to retrieve the current Org ID being viewed.
https://i.sstatic.net/NydWb.png
Image 2: Firebase throws an error
Here is the Organization Group (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 function reads the Organization Group ID
this.orgId = this.activateRoute.snapshot.params['orgID'];
// Calling the method and passing the Org ID
this.postService.getOrgId(this.orgId);
this.orgSub = this.postService.getPosts().subscribe(posts => {
this.posts = posts;
this.isLoading = false;
});
}
And here is the Post Service (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"));
//The Organization Group ID is used in the WHERE Clause for filtering
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;
})
})
);
}//
//Method to retrieve all posts
getPosts() {
return this.posts;
}
//Function to obtain the Organization ID
getOrgId(idParameter): Observable<any>{
return this.orgId = this.afs.collection('post', result => result.where('postOrgId', '==', idParameter)).valueChanges();
}