Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Does not return collection data when more than one query constraint #540

Closed
5 of 16 tasks
Tsquared86 opened this issue Jan 7, 2024 · 5 comments
Closed
5 of 16 tasks
Labels

Comments

@Tsquared86
Copy link

Plugin(s)

  • Analytics
  • App
  • App Check
  • Authentication
  • Crashlytics
  • Cloud Firestore
  • Cloud Messaging
  • Cloud Storage
  • Performance
  • Remote Config

Version

Latest

Platform(s)

  • Android
  • iOS
  • Web

Current behavior

I am returning data from a collection and using the query constraint "and" to compare multiple fields. This works fine on all platforms, but when on android this does not return data.

Expected behavior

IT should work like on web and iOS returning data.

Reproduction

https://github.com/tsquared86

Steps to reproduce

try to do a query of a collection with an and comparison of 2 or more constraints

Other information

No response

Capacitor doctor

Latest Dependencies:

@capacitor/cli: 5.6.0
@capacitor/core: 5.6.0
@capacitor/android: 5.6.0
@capacitor/ios: 5.6.0

Installed Dependencies:

@capacitor/cli: 5.0.4
@capacitor/core: 5.0.4
@capacitor/android: 5.0.4
@capacitor/ios: 5.0.4

Before submitting

  • I have read and followed the bug report guidelines.
  • I have attached links to possibly related issues and discussions.
  • I understand that incomplete issues (e.g. without reproduction) are closed.
@Tsquared86 Tsquared86 added bug/fix Something isn't working needs: triage labels Jan 7, 2024
@robingenz robingenz added this to the v5.4.0 milestone Jan 7, 2024
@orestis-z
Copy link

+1

@robingenz robingenz modified the milestones: v5.4.0, v5.5.0 Jan 23, 2024
@jwuliger
Copy link

Hello, I would like to chime in here. I have a query issue on Android that has me running in circles. In my Ionic/Capacitor Angular application for Android, I have a method to load collection data by the user who is signed in.

    async loadConversations(userId: string) {
        try {
            const queryConstraints = [
                {
                    type: 'where',
                    fieldPath: 'userId',
                    opStr: '==',
                    value: userId
                },
                {
                    type: 'orderBy',
                    fieldPath: 'timestamp',
                    directionStr: 'desc'
                }
            ];

            const snapshots = await this.firestoreService.getCollection(
                'conversations',
                queryConstraints
            );
            this.conversations = snapshots.map((doc) => {
                const data = doc.data as Partial<Conversation>;
                return { id: doc.id, ...data };
            });
        } catch (error) {
            console.error('Error loading conversations:', error);
        }
    }

The problem started when I added 'where' query. When run in the browser it first asked me to create a composite query index in Firebase to run the query. Once I added the new index, I saw the data load on that page.

When I run the same code on my Android Phone I get this error:

Error: Attempt to invoke interface method 'com.google.firebase.firestore.Query io.capawesome.capacitorjs.plugins.firebase.firestore.interfaces.QueryNonFilterConstraint.toQuery(com.google.firebase.firestore.Query, com.google.firebase.firestore.FirebaseFirestore)' on a null object reference

the 'getCollection()' method is in a service and the method is:

    async getCollection(reference: string, queryConstraints: any[]) {
        const { snapshots } = await FirebaseFirestore.getCollection({
            reference,
            queryConstraints
        });
        return snapshots;
    }

I have no idea what the null object reference could be.

Here is what my index looks like from the Firebase Console

2024-01-30_8-41-36

Any help would be greatly appreciated. Thanks for your time. Please let me know if I can provide anything further. This is a private code base so I can't link the repo.

@robingenz
Copy link
Member

I will probably work on the Firestore issues next week.

@jwuliger
Copy link

Update:

I modified FirebaseFirestore.java > getCollection() method. I added a null check for the queryConstraint

Here is the method that works on my Android Device:

    public void getCollection(@NonNull GetCollectionOptions options, @NonNull NonEmptyResultCallback callback) throws Exception {
        String reference = options.getReference();
        QueryCompositeFilterConstraint compositeFilter = options.getCompositeFilter();
        QueryNonFilterConstraint[] queryConstraints = options.getQueryConstraints();

        Query query = this.firestoreInstance.collection(reference);
        if (compositeFilter != null) {
            Filter filter = compositeFilter.toFilter();
            assert filter != null;
            query = query.where(filter);
        }
        for (QueryNonFilterConstraint queryConstraint : queryConstraints) {
            if (queryConstraint != null) { // Add this null check
                query = queryConstraint.toQuery(query, this.firestoreInstance);
            } else {
                // Optionally handle the null case, e.g., log an error
                Log.e("FirebaseFirestore", "Encountered null QueryNonFilterConstraint");
            }
        }
        query
            .get()
            .addOnSuccessListener(
                querySnapshot -> {
                    GetCollectionResult result = new GetCollectionResult(querySnapshot);
                    callback.success(result);
                }
            )
            .addOnFailureListener(callback::error);
    }

I hope this helps.

@robingenz
Copy link
Member

@Tsquared86 I close this issue as duplicate of #554.

@jwuliger Please create a new issue and provide some code to reproduce this issue. I do not know the implementation of your firestoreService.

@robingenz robingenz closed this as not planned Won't fix, can't repro, duplicate, stale Feb 14, 2024
@robingenz robingenz removed this from the v5.5.0 milestone Feb 14, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants