From the https://firebase.google.com/docs/firestore/query-data/queries documentation snippet, Im unable to get the results as an Observable
import { collectionGroup, query, where, getDocs } from "firebase/firestore";
const museums = query(collectionGroup(db, 'landmarks'), where('type', '==', 'museum'));
const querySnapshot = await getDocs(museums);
querySnapshot.forEach((doc) => {
console.log(doc.id, ' => ', doc.data());
});
So I wrote it like this:
const museums = query(
collectionGroup(db, 'landmarks'),
where('type', '==', 'museum')
);
const querySnapshot = await getDocs(museums);
const datas = querySnapshot.docs.map((doc) => {
return doc.data() as Data;
});
console.log(datas);
return of(datas);
But I’m not getting results when subscribing to it or in the DOM using async pipe, but when I console.log(), there is an array of data.
component.html
{{ museums_data$ | async }}
component.ts
this.museums_data$.subscribe((datas) => console.log(datas));
2
Answers
I found it here: https://code.build/p/LWmSi3HfQzu5TMKPzb5p6i/angular-12-with-firebase-9, which is written for the collection. Assuming it should also work for collection groups, I tried it, and it performed as expected.
This is a code snippet found in the link:
to:
At First glance code looks good but you are returning it incorrectly. Instead of mapping over you can return it directly using
as Observable<Data[]>
. Although I have not considered the error handling this can easily be achieved using thecatchError
rxjs operator.You can use the following technique to get the data as you want using an
async
pipe.data.service.ts :
And use it like this in the corresponding component:
app.component.ts :
And
app.component.html :
For more information there is this article that explains nicely.