invertase / react-native-firebase

🔥 A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.72k stars 2.22k forks source link

[🐛] firestore onSnapshot returns undefined on certain compound Where queries #7560

Closed SamJbori closed 7 months ago

SamJbori commented 10 months ago

Issue

When I try to do compound where with anything other than "==", I get an undefined querySnapshot, see below code example

Failed Scenario

import firestore from "@react-native-firebase/firestore";
const collections = {
  users: firestore().collection("Users"),
};

  useEffect(() => {
    if (userCurrent && !userCurrent.isAnonymous) {
     try {
        const subscriber = [
          collections.users
            .where("users", "array-contains", userCurrent.uid)
            .where("created", "<=", 1700000000000)  // using "==" works just fine
            .onSnapshot({includeMetadataChanges: true}, (snapShot) => {
              console.log(snapShot); //  results undefined
            }
          ]
         return () => subscriber.forEach((sub) =>sub());
     } catch (e){}
  }
  }, [userCurrent]);

Project Files

Javascript

Click To Expand

#### `package.json`: ```json { "name": "XXX", "version": "1.0.0", "main": "node_modules/expo/AppEntry.js", "scripts": { "start": "expo start", "android": "expo run:android", "ios": "expo run:ios", "web": "expo start --web", "dev": "expo start --dev-client" }, "dependencies": { "@react-native-community/hooks": "^3.0.0", "@react-native-firebase/app": "^18.6.1", "@react-native-firebase/auth": "^18.6.1", "@react-native-firebase/crashlytics": "^18.6.1", "@react-native-firebase/firestore": "^18.6.1", "@react-native-firebase/messaging": "^18.6.1", "@react-native-firebase/perf": "^18.6.1", "@react-native-firebase/storage": "^18.6.1", "@react-native-picker/picker": "2.4.10", "@react-navigation/bottom-tabs": "^6.5.11", "@react-navigation/drawer": "^6.6.6", "@react-navigation/material-top-tabs": "^6.6.5", "@react-navigation/native": "^6.1.9", "@react-navigation/native-stack": "^6.9.17", "@react-navigation/stack": "^6.3.20", "@shopify/flash-list": "1.4.3", "@stripe/stripe-react-native": "^0.35.0", "axios": "^1.6.2", "expo": "~49.0.15", "expo-barcode-scanner": "~12.5.3", "expo-image": "~1.3.5", "expo-image-manipulator": "~11.3.0", "expo-image-picker": "~14.3.2", "expo-module-scripts": "^3.1.0", "expo-splash-screen": "~0.20.5", "expo-status-bar": "~1.6.0", "i18next": "^23.7.6", "jotai": "^2.5.1", "lodash": "^4.17.21", "react": "18.2.0", "react-i18next": "^13.5.0", "react-native": "0.72.6", "react-native-element-dropdown-wj": "^2.10.1-a", "react-native-gesture-handler": "~2.12.0", "react-native-localize": "^3.0.3", "react-native-pager-view": "6.2.0", "react-native-paper": "^5.11.1", "react-native-reanimated": "~3.3.0", "react-native-safe-area-context": "4.6.3", "react-native-screens": "~3.22.0", "react-native-svg": "13.9.0", "react-native-tab-view": "^3.5.2" }, "devDependencies": { "@babel/core": "^7.20.0", "@react-native-community/geolocation": "^3.1.0", "@types/lodash": "^4.14.202", "@types/node": "^20.9.4", "@types/react": "~18.2.14", "eslint-plugin-prettier": "^5.0.1", "prettier": "3.1.0", "react-native-vector-icons": "^10.0.3", "typescript": "^5.1.3" }, "private": true } ``` #### `firebase.json` for react-native-firebase v6: ```json { "react-native": { "android_task_executor_maximum_pool_size": 10, "android_task_executor_keep_alive_seconds": 3 } } ```

iOS

Click To Expand

#### `ios/Podfile`: - [ ] I'm not using Pods - [x] I'm using Pods and my Podfile looks like: ```ruby # N/A ``` #### `AppDelegate.m`: ```objc // N/A ```


Android

Click To Expand

#### Have you converted to AndroidX? - [ ] my application is an AndroidX application? - [ ] I am using `android/gradle.settings` `jetifier=true` for Android compatibility? - [ ] I am using the NPM package `jetifier` for react-native compatibility? #### `android/build.gradle`: ```groovy // N/A ``` #### `android/app/build.gradle`: ```groovy // N/A ``` #### `android/settings.gradle`: ```groovy // N/A ``` #### `MainApplication.java`: ```java // N/A ``` #### `AndroidManifest.xml`: ```xml ```


Environment

Click To Expand

**`react-native info` output:** ``` System: OS: Linux 6.2 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish) CPU: (12) x64 13th Gen Intel(R) Core(TM) i9-13900K Memory: 8.52 GB / 15.58 GB Shell: version: 5.1.16 path: /bin/bash Binaries: Node: version: 18.19.0 path: ~/.nvm/versions/node/v18.19.0/bin/node Yarn: version: 1.22.19 path: ~/.nvm/versions/node/v18.19.0/bin/yarn npm: version: 10.2.3 path: ~/.nvm/versions/node/v18.19.0/bin/npm Watchman: Not Found SDKs: Android SDK: API Levels: - "33" - "34" Build Tools: - 30.0.3 - 33.0.0 - 34.0.0 System Images: - android-34 | Google APIs Intel x86_64 Atom Android NDK: Not Found IDEs: Android Studio: Not Found Languages: Java: version: 18.0.2-ea path: /usr/bin/javac Ruby: Not Found npmPackages: "@react-native-community/cli": Not Found react: installed: 18.2.0 wanted: 18.2.0 react-native: installed: 0.72.6 wanted: 0.72.6 npmGlobalPackages: "*react-native*": Not Found Android: hermesEnabled: true newArchEnabled: false iOS: hermesEnabled: Not found newArchEnabled: Not found``` - **Platform that you're experiencing the issue on**: - [ ] iOS - [ ] Android - [ ] **iOS** but have not tested behavior on Android - [X] **Android** but have not tested behavior on iOS - [ ] Both - **`react-native-firebase` version you're using that has this issue:** - `18.6.1` - **`Firebase` module(s) you're using that has the issue:** - `e.g. Instance ID` - **Are you using `TypeScript`?** - `Y` & `5.2.2`


mikehardy commented 9 months ago

🤔 hmm - my guess is you are missing an index:

https://firebase.google.com/docs/firestore/query-data/queries#compound_and_queries

Do you see any errors in the logs about missing indices?

github-actions[bot] commented 8 months ago

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

SamJbori commented 8 months ago

🤔 hmm - my guess is you are missing an index:

https://firebase.google.com/docs/firestore/query-data/queries#compound_and_queries

Do you see any errors in the logs about missing indices?

Sorry for the late response,

the answer to your question is no

github-actions[bot] commented 7 months ago

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

yuvalhermelin-fijoya commented 7 months ago

Why is this stale? He isn't missing an index according to him and the snapshot is still providing its callback with an undefined value.

I am experiencing this too - sometimes for unclear reasons the snapshot is undefined and thus its not enough to check snapshot.empty or .exists (as this will result in an error).

We started checking if the snapshot is defined as part of all our callback structures - but in what situations does onSnapshot return undefined @mikehardy ? Thanks!

mikehardy commented 7 months ago

Hey @yuvalhermelin-fijoya - the stale bot is not really mysterious, it is marked stale because there has not been any activity - as an open source repository it is possible for anyone to investigate problems by instrumenting the code, trying fixes, and posting a PR, but no one has done so yet

I'm not sure the answer to your question, and I'm not sure if what you're experiencing is the same or not - the reproduction scenario the original user posted appears to reproduce the problem all the time. You indicate you only have this happen some of the time so you may have a different issue.

This appears to be the code in question:

https://github.com/invertase/react-native-firebase/blob/0023090d3056ff4dee3c12da5eb2cca453cb9c6e/packages/firestore/lib/FirestoreDocumentReference.js#L123-L174

When you reproduce the problem after instrumenting the code with some console.log statements indicating where control flow is and what the variable states are, what do you see?

MrLibya commented 5 months ago

This happen to me too, it was direct document onSnapshot:

collectionName()
            .doc('docA')
            .onSnapshot(snap => {})

What happen here is I got the data at first then the function got trigger again and I got undefined ! Also I deleted the document docA and was still getting the old data then undefined ! ( and the document doesn't exist in firebase ), and to make sure I tried to get the data using doc normal function and it says doesn't exist.

Note it took sometimes ( I think over 30m ) so that onSnapshot got that document doesn't exist !

So do we have some kind of cache in the package ? maybe its related ?

I haven't made any debug as I was hurry but will do later for sure !