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

[🐛] 🔥 firestore keeps calling cloud store instead of emulated one #6530

Closed harijoe closed 2 years ago

harijoe commented 2 years ago

Issue

I followed the documentation instructions :

import { registerRootComponent } from 'expo';

import '@react-native-firebase/app';
import functions from '@react-native-firebase/functions';
import firestore from '@react-native-firebase/firestore';

if (__DEV__) {
  const HOST = '172.20.10.3';

  functions().useEmulator(HOST, 5001);
  firestore().useEmulator(HOST, 8080);

  firestore().collection('users').add({ hello: 'world' });
}

import App from './src/App';

// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
// It also ensures that whether you load the app in Expo Go or in a native build,
// the environment is set up appropriately
registerRootComponent(App);

I started the emulators (http://localhost:4000/firestore/data working as expected) but users keep being added to the cloud project and not to the emulated firestore. The line firestore().useEmulator(HOST, 8080) seems completely ignored.

My cloud firestore is located at eur3 (europe-west) (maybe it will help)


Project Files

Javascript

Click To Expand

#### `package.json`: ```json { "name": "myproject", "version": "1.0.0", "scripts": { "start": "expo start --dev-client", "android": "expo run:android", "ios": "expo run:ios", "web": "expo start --web", "prepare": "husky install" }, "dependencies": { "@react-native-async-storage/async-storage": "~1.17.3", "@react-native-community/hooks": "^2.8.1", "@react-native-firebase/app": "^15.4.0", "@react-native-firebase/auth": "^15.4.0", "@react-native-firebase/firestore": "^15.4.0", "@react-native-firebase/functions": "^15.4.0", "@react-native-firebase/messaging": "^15.4.0", "@react-native-firebase/storage": "^15.4.0", "@react-native-ml-kit/text-recognition": "^1.2.0", "@react-navigation/bottom-tabs": "^6.3.3", "@react-navigation/native": "^6.0.12", "@react-navigation/native-stack": "^6.8.0", "ahooks": "^3.7.0", "expo": "~46.0.9", "expo-asset": "~8.6.1", "expo-build-properties": "~0.3.0", "expo-camera": "~12.3.0", "expo-constants": "~13.2.3", "expo-contacts": "~10.3.0", "expo-dev-client": "~1.2.1", "expo-font": "^10.2.0", "expo-linear-gradient": "^11.4.0", "expo-splash-screen": "~0.16.2", "expo-status-bar": "~1.4.0", "lodash": "^4.17.21", "nanoid": "^4.0.0", "react": "18.0.0", "react-dom": "18.0.0", "react-firebase-hooks": "^3.0.4", "react-hook-form": "^7.34.2", "react-native": "0.69.5", "react-native-animatable": "^1.3.3", "react-native-fast-image": "^8.6.1", "react-native-get-random-values": "^1.8.0", "react-native-image-resizer": "^1.4.5", "react-native-keyboard-aware-scroll-view": "^0.9.5", "react-native-safe-area-context": "^4.3.3", "react-native-screens": "~3.15.0", "react-native-shadow-2": "^7.0.5", "react-native-svg": "12.3.0", "react-native-user-avatar": "^1.0.8", "react-native-web": "~0.18.8", "react-phone-number-input": "^3.2.10", "uuid": "^8.3.2" }, "devDependencies": { "@babel/core": "^7.12.9", "@types/lodash": "^4.14.185", "@types/react": "~18.0.14", "@types/react-native": "~0.69.1", "husky": "^6.0.0", "lint-staged": "^11.0.0", "prettier": "^2.5.1", "standard-version": "^9.3.0", "typescript": "~4.8.2" }, "private": true, "lint-staged": { "**/*.ts?(x)": "prettier --write" } } ``` #### `firebase.json` for react-native-firebase v6: ```json { "react-native": { "messaging_ios_auto_register_for_remote_messages": false }, "functions": { "source": "functions" }, "emulators": { "auth": { "host": "0.0.0.0", "port": 9099 }, "functions": { "host": "0.0.0.0", "port": 5001 }, "firestore": { "host": "0.0.0.0", "port": 8080 }, "storage": { "host": "0.0.0.0", "port": 9199 }, "ui": { "enabled": true } }, "firestore": { "rules": "firestore.rules", "indexes": "firestore.indexes.json" }, "storage": { "rules": "storage.rules" } } ```

iOS

Click To Expand

#### `ios/Podfile`: - [ ] I'm not using Pods - [x] I'm using Pods and my Podfile looks like: ```ruby require File.join(File.dirname(`node --print "require.resolve('expo/package.json')"`), "scripts/autolinking") require File.join(File.dirname(`node --print "require.resolve('react-native/package.json')"`), "scripts/react_native_pods") require File.join(File.dirname(`node --print "require.resolve('@react-native-community/cli-platform-ios/package.json')"`), "native_modules") require 'json' podfile_properties = JSON.parse(File.read(File.join(__dir__, 'Podfile.properties.json'))) rescue {} platform :ios, podfile_properties['ios.deploymentTarget'] || '12.4' install! 'cocoapods', :deterministic_uuids => false target 'mygarageexpo' do use_expo_modules! config = use_native_modules! use_frameworks! :linkage => podfile_properties['ios.useFrameworks'].to_sym if podfile_properties['ios.useFrameworks'] # Flags change depending on the env values. flags = get_default_flags() use_react_native!( :path => config[:reactNativePath], :hermes_enabled => flags[:hermes_enabled] || podfile_properties['expo.jsEngine'] == 'hermes', :fabric_enabled => flags[:fabric_enabled], # An absolute path to your application root. :app_path => "#{Dir.pwd}/.." ) # Uncomment to opt-in to using Flipper # Note that if you have use_frameworks! enabled, Flipper will not work # # if !ENV['CI'] # use_flipper!() # end post_install do |installer| react_native_post_install(installer) __apply_Xcode_12_5_M1_post_install_workaround(installer) end post_integrate do |installer| begin expo_patch_react_imports!(installer) rescue => e Pod::UI.warn e end end end ``` #### `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: macOS 12.5 CPU: (10) arm64 Apple M1 Max Memory: 1.66 GB / 32.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 16.15.1 - /opt/homebrew/bin/node Yarn: 1.22.19 - /opt/homebrew/bin/yarn npm: 8.11.0 - /opt/homebrew/bin/npm Watchman: Not Found Managers: CocoaPods: 1.11.3 - /opt/homebrew/bin/pod SDKs: iOS SDK: Platforms: DriverKit 21.4, iOS 15.5, macOS 12.3, tvOS 15.4, watchOS 8.5 Android SDK: Not Found IDEs: Android Studio: Not Found Xcode: 13.4.1/13F100 - /usr/bin/xcodebuild Languages: Java: 18.0.1.1 - /opt/homebrew/opt/openjdk/bin/javac npmPackages: @react-native-community/cli: Not Found react: 18.0.0 => 18.0.0 react-native: 0.69.5 => 0.69.5 react-native-macos: Not Found npmGlobalPackages: *react-native*: Not Found ``` - **Platform that you're experiencing the issue on**: - [ ] iOS - [ ] Android - [ ] **iOS** but have not tested behavior on Android - [ ] **Android** but have not tested behavior on iOS - [ X] Both - **`react-native-firebase` version you're using that has this issue:** - `15.4.0` - **`Firebase` module(s) you're using that has the issue:** - `e.g. Instance ID` - **Are you using `TypeScript`?** - `Y`


mikehardy commented 2 years ago

Very strange indeed - I have been using the firestore emulator extensively in a work project all week including today, and our tests app also uses it. It works without issue, so I'm not sure what the problem might be.

That's not a great answer, as it is not definitive, but it means I do not reproduce this, and our tests project here (100% open, runs frequently, you may investigate it at will...) also does not reproduce.

There must be something project-specific going on. It might be best to make a quick "toy" like https://github.com/mikehardy/rnfbdemo/blob/main/make-demo.sh as a starter then start up the emulator and have it connect to that for a quick firebase test to make sure it works.

harijoe commented 2 years ago

I just noticed I tested it on a real device (iphone 8) using expo managed workflow. Could it be related ? I will test later today with the ios simulator and edit my answer accordingly.

EDIT : I confirm, it works on the ios simulator, not on a real device EDIT 2 : I think the problem comes from the fact that in the expo managed workflow, it's needed to build the app and send it to the emulator/the real device used for testing when a dependency is added for example to load the new native dependencies. The index.js file may also be sent only during a build and not through the dev server as the other typescript files, so I may have experienced the "useEmulator is ignored" bug because the running index.js file was an old version I built and sent to my device, and not the actual file in my mac filesystem.

mikehardy commented 2 years ago

Okay - I'm going to close this as not actionable since I think everything is working - we have not experienced this and it appears you've got a reasonable guess at your root cause + you can see it now. If there is something reproducible where connecting to the emulator fails of course, outline the steps to reproduce and we can re-examine. Good luck with your project!