Skip to content

Commit

Permalink
fix(firestore): getCollectionGroup(...) returns no data (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz authored Jan 4, 2024
1 parent 66c3007 commit 2b1d4d7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-dots-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/firestore': patch
---

fix(web): `getCollectionGroup(...)` returns no data
32 changes: 23 additions & 9 deletions packages/firestore/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
and,
clearIndexedDbPersistence,
collection,
collectionGroup,
deleteDoc,
disableNetwork,
doc,
Expand Down Expand Up @@ -128,7 +129,10 @@ export class FirebaseFirestoreWeb
public async getCollection<T extends DocumentData>(
options: GetCollectionOptions,
): Promise<GetCollectionResult<T>> {
const collectionQuery = await this.buildCollectionQuery(options);
const collectionQuery = await this.buildCollectionQuery(
options,
'collection',
);
const collectionSnapshot = await getDocs(collectionQuery);
return {
snapshots: collectionSnapshot.docs.map(documentSnapshot => ({
Expand All @@ -142,7 +146,10 @@ export class FirebaseFirestoreWeb
public async getCollectionGroup<T extends DocumentData>(
options: GetCollectionGroupOptions,
): Promise<GetCollectionGroupResult<T>> {
const collectionQuery = await this.buildCollectionQuery(options);
const collectionQuery = await this.buildCollectionQuery(
options,
'collectionGroup',
);
const collectionSnapshot = await getDocs(collectionQuery);
return {
snapshots: collectionSnapshot.docs.map(documentSnapshot => ({
Expand Down Expand Up @@ -200,7 +207,10 @@ export class FirebaseFirestoreWeb
options: AddCollectionSnapshotListenerOptions,
callback: AddCollectionSnapshotListenerCallback<T>,
): Promise<string> {
const collectionQuery = await this.buildCollectionQuery(options);
const collectionQuery = await this.buildCollectionQuery(
options,
'collection',
);
const unsubscribe = onSnapshot(collectionQuery, snapshot => {
const event: AddCollectionSnapshotListenerCallbackEvent<T> = {
snapshots: snapshot.docs.map(documentSnapshot => ({
Expand Down Expand Up @@ -240,20 +250,22 @@ export class FirebaseFirestoreWeb
| GetCollectionOptions
| GetCollectionGroupOptions
| AddCollectionSnapshotListenerOptions,
type: 'collection' | 'collectionGroup',
): Promise<Query<DocumentData, DocumentData>> {
const firestore = getFirestore();
let collectionQuery: Query;
if (options.compositeFilter) {
const compositeFilter =
await this.buildFirebaseQueryCompositeFilterConstraint(
options.compositeFilter,
);
const compositeFilter = this.buildFirebaseQueryCompositeFilterConstraint(
options.compositeFilter,
);
const queryConstraints =
await this.buildFirebaseQueryNonFilterConstraints(
options.queryConstraints || [],
);
collectionQuery = query(
collection(firestore, options.reference),
type === 'collection'
? collection(firestore, options.reference)
: collectionGroup(firestore, options.reference),
compositeFilter,
...queryConstraints,
);
Expand All @@ -262,7 +274,9 @@ export class FirebaseFirestoreWeb
options.queryConstraints || [],
);
collectionQuery = query(
collection(firestore, options.reference),
type === 'collection'
? collection(firestore, options.reference)
: collectionGroup(firestore, options.reference),
...queryConstraints,
);
}
Expand Down

0 comments on commit 2b1d4d7

Please sign in to comment.