Warm tip: This article is reproduced from serverfault.com, please click

Firebase throws ERROR FirebaseApp name [DEFAULT] already exists

发布于 2020-11-28 10:11:14

Here is my FirebaseSetup.js File

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}
}

when I comment the above mentioned lines the app works fine but is not connected to my firebaseDB

When I uncomment the above mentioned line it Throws ERROR that says.....

FirebaseApp name [DEFAULT] already exists! besides the FIREBASE INSTANCES shows 0 as length..

Atlast i Understood that myself can't able to initialize App.. why?

Questioner
Kavinkumar . N
Viewed
11
Emanuele Scarabattoli 2020-11-28 19:15:21

You have to check the firebase apps length in this way:

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

Note that the property is in plural, apps and not app.

More information cand by found here:

https://rnfirebase.io/reference/app

Note that both app and apps are valid properties but you actually need the apps one, since what you are checking for is the length of array of apps.