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.64k stars 2.21k forks source link

[πŸ›] πŸ”₯ Realtime DB Reference .once() returns a String instead of a Reference #7395

Closed immersify-studio closed 11 months ago

immersify-studio commented 11 months ago

Issue

Describe your issue here

I am switching over to react-native-firebase libraries from the web based libraries like firebase/database and have been banging my head on this. From what I can tell, my app is initialized correctly and creates the correct reference to my database. When I print out the reference, it has the correct path to the database where I would like to pull, however, it's a string (i.e. https:///. I would expect this to be a Reference type. In turn, this causes my code to fail when calling .once('value'), claiming this is undefined. Is there anything why this would return a string instead of a Reference object?

import database from '@react-native-firebase/database';
import firebase from '@react-native-firebase/app';

let app: any;

if (!firebase.apps.length) {
  app = firebase.initializeApp(firebaseConfig);
} else {
  app = firebase.apps[0];
}

const realTimeDB = database();

export async function getUserDataById(userId: string): Promise<any> {
  console.log("getUserDataById");
  try {
    console.log("realtimeDB", realTimeDB)
    const reference = database(app).ref('users').child(userId);
    console.log('reference', reference);

    const snapshot = await reference.once('value');
    console.log("snapshot", snapshot)

    if (snapshot.exists()) {
      return snapshot.val();
    } else {
      return null;
    }
  } catch (error) {
    console.log("user", auth().currentUser);
    console.error(error);
    return null;
  }
}

Project Files

Javascript

Click To Expand

#### `package.json`: ```json { "@react-native-firebase/analytics": "18.4.0", "@react-native-firebase/app": "18.4.0", "@react-native-firebase/database": "18.4.0", "@react-native-firebase/auth": "18.4.0", "@react-native-firebase/crashlytics": "18.4.0", "@react-native-firebase/dynamic-links": "18.4.0", "@react-native-firebase/firestore": "18.4.0", "@react-native-firebase/functions": "18.4.0", "@react-native-firebase/messaging": "18.4.0", "@react-native-firebase/remote-config": "18.4.0", "@react-native-firebase/storage": "18.4.0" } ``` #### `firebase.json` for react-native-firebase v6: ```json # N/A ```

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? - [X] 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:** ``` OUTPUT GOES HERE ``` - **Platform that you're experiencing the issue on**: - [ ] iOS - [ ] Android - [X] **iOS** but have not tested behavior on Android - [ ] **Android** but have not tested behavior on iOS - [ ] Both - **`react-native-firebase` version you're using that has this issue:** - `e.g. 18.4.0` - **`Firebase` module(s) you're using that has the issue:** - `e.g. Instance ID` - **Are you using `TypeScript`?** - `Y` & `5.1.6`


mikehardy commented 11 months ago

Sorry for the delay - πŸ€” Whenever I am stuck on usage, I refer to the e2e tests here, which usually serve as a good example of how to use things.

I wonder if the problem is that some of our APIs are async vs firebase-js-sdk? We have to do that in many cases to handle the asynchronous nature of the react-native JS/native bridge

Here is an example using .once that may serve the purpose for you? I don't think this will result in a change in this package so I'm going to close this, but I do hope this helps and the general pointer to the e2e suite for examples really helps:

https://github.com/invertase/react-native-firebase/blob/2d8ac144a14e79ce1846ecab077f8d7b69d11f05/packages/database/e2e/query/once.e2e.js#L87-L97