Firebase throws ERROR FirebaseApp name [DEFAULT] already exists

Viewed 171

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?

1 Answers

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.

Related