Firebase 抛出错误 FirebaseApp 名称 [DEFAULT] 已经存在

Firebase throws ERROR FirebaseApp name [DEFAULT] already exists

这是我的 FirebaseSetup.js 文件

import React from 'react';
import firebase from '@react-native-firebase/app';
import auth from '@react-native-firebase/auth';

const firebaseConfig = {
    apiKey: "MyApi",
    authDomain: "MyAuthDomain",
    databaseURL: "DBurl",
    projectId: "PID",
    storageBucket: "...",
    messagingSenderId: "...",
    appId: "...."
}

if(!firebase.app.length){                   //The Problem
    firebase.initializeApp(firebaseConfig); //arises in
    console.log('FireBase Initialized');    //these lines
}

console.log(`FIREBASE INSTANCES : => ${firebase.app.length}`);

export default () => {
    return {firebase,auth}
}

当我评论上面提到的行时,该应用程序运行正常但未连接到我的 firebaseDB

当我取消对上面提到的行的注释时,它会抛出错误...

FirebaseApp name [DEFAULT] already exists! 除了 FIREBASE INSTANCES 显示 0 作为长度..

最后我明白了我自己无法初始化应用程序..为什么?

您必须以这种方式检查 firebase 应用程序的长度:

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

请注意 属性 是复数,apps 而不是 app

可在此处找到更多信息:

https://rnfirebase.io/reference/app

请注意,appapps 都是有效属性,但您实际上需要 apps 一个,因为您要检查的是 [=11= 数组的长度].