I'm using @angular/fire to fetch data from firestore. I have two components. one is parent and the other is child. Both of these components are subscribing to observable using async pipes. These are two different observables (but on the same collection). When i arrive on the child route i can see the data from parent route is there until the child component's observable gives response and replaces the data that shouldn't be there. Why is this happening?
This is the method that returns an observable which is used the parent component.
getMyTasks(workspaceID, userID) {
return this.afs.collection('Tasks', ref => ref.where(`workspaceID`, '==', workspaceID).where(`members.${userID}`, '==', true))
.snapshotChanges().pipe(
map(actions => actions.map(a => {
const data = a.payload.doc.data() as Task;
const id = a.payload.doc.id;
let member = data.members;
let isMember = false;
for (var b in Object.keys(data.members)) {
if (Object.keys(member)[b] === userID) {
isMember = true;
break;
} else {
isMember = false;
}
}
let createdBy = data.createdBy;
let creatorName = "";
for (let i = 0; i < this.members.length; i++) {
if (this.members[i].memberId === createdBy) {
creatorName = this.members[i].data.name;
}
}
return { id, creatorName, isMember, ...data };
}))
);
}
this.tasks$ = this.firestoreService.getMyTasks(this.workspaceID, this.userID);
And then i'm using async pipe in the template. And here is the method which is used in child component
getTasks(projectId, members) {
return this.afs.collection('Tasks', ref => ref.where('projectId', '==', projectId).orderBy("createdAt", "desc"))
.snapshotChanges().pipe(map(actions => actions.map(a => {
const data = a.payload.doc.data() as Task;
const docId = a.payload.doc.id;
let createdBy = data.createdBy;
let creatorName = "";
for (let i = 0; i < members.length; i++) {
if (members[i].memberId === createdBy) {
creatorName = members[i].data.name;
}
}
return { docId, ...data, creatorName }
})))}
this is child component.ts
this.tasks = this.firestoreService.getTasks(this.projectId, this.members);