【问题标题】:Why is firebaseApp already initialized in React Native on Android?为什么 firebaseApp 已经在 Android 上的 React Native 中初始化了?
【发布时间】:2020-07-30 03:38:25
【问题描述】:

当我尝试在 android 上运行此代码时,我收到错误 FirebaseApp name [DEFAULT] already exists! 在 this thread 中,它说 firebaseApp 在应用程序启动时被初始化,但是为什么你可以像在 this thread 中那样调用 initializeApp?看起来我的代码在功能上与第二个线程中的代码相似,那么为什么会出现错误?它在 iOS 上运行良好,所以我完全迷路了。我错过了什么?

编辑:忘了提到所有 testConfig 值都存储在一个 .env 文件中,该文件可由 react-native-config 库访问。

编辑 2:将 firebase.initializeApp(testConfig); 更改为 firebase.initializeApp(testConfig, 'fire'); 修复了 android,但破坏了 ios,导致错误 No Firebase App '[DEFAULT]' has been created - call firebase.initializeApp()

代码:

import firebase from '@react-native-firebase/app';
import '@react-native-firebase/auth';        // for authentication
import '@react-native-firebase/storage';     // for storage
import '@react-native-firebase/firestore';   // for cloud firestore
import '@react-native-firebase/functions';   // for cloud functions
import '@react-native-firebase/messaging';   // for cloud messaging
import Config from 'react-native-config'

const testConfig = {
  apiKey: Config.REACT_APP_FIREBASE_TEST_API_KEY,
  authDomain: Config.REACT_APP_FIREBASE_TEST_AUTH_DOMAIN,
  databaseURL: Config.REACT_APP_FIREBASE_TEST_DATABASE_URL,
  projectId: Config.REACT_APP_FIREBASE_TEST_PROJECT_ID,
  storageBucket: Config.REACT_APP_FIREBASE_TEST_STORAGE_BUCKET,
  messagingSenderId: Config.REACT_APP_FIREBASE_TEST_MESSAGING_SENDER_ID,
  appId: Config.REACT_APP_FIREBASE_TEST_APP_ID,
  measurementId: Config.REACT_APP_FIREBASE_TEST_MEASUREMENT_ID
};

const fire = firebase.initializeApp(testConfig);

Logcat:

2020-04-16 11:05:17.269 25458-25543/com.nativeminutetech E/unknown:ReactNative: Exception in native call
    java.lang.IllegalStateException: FirebaseApp name [DEFAULT] already exists!
        at com.google.android.gms.common.internal.Preconditions.checkState(Unknown Source:29)
        at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.3.0:295)
        at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.3.0:268)
        at io.invertase.firebase.common.RCTConvertFirebase.readableMapToFirebaseApp(RCTConvertFirebase.java:97)
        at io.invertase.firebase.app.ReactNativeFirebaseAppModule.initializeApp(ReactNativeFirebaseAppModule.java:55)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
        at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:151)
        at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
        at android.os.Looper.loop(Looper.java:164)
        at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:226)
        at java.lang.Thread.run(Thread.java:764)

【问题讨论】:

    标签: android reactjs firebase react-native


    【解决方案1】:

    是的,别忘了检查:

    if (!firebase.apps.length) {
          firebase.initializeApp(testConfig);
    }
    

    【讨论】:

      【解决方案2】:

      正如react-native-firebase docs 中所述,Firebase 会在应用启动时自动初始化,所以我最初的initializeApp 调用是多余的。它没有为 iOS 初始化的原因是我忽略了在 Firebase 控制台中为 iOS 设置一个应用程序(oops)。如果其他人遇到这个问题,请注意您需要已经为这两个操作系统完成了设置,只有一个不能解决问题。

      【讨论】:

        猜你喜欢
        • 2018-11-21
        • 1970-01-01
        • 2015-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-11
        • 1970-01-01
        相关资源
        最近更新 更多