0

I am trying to use Firebase in an app component. This is how the code looks like for that component (removed the api details from json while posting here).

import React from "react";
import * as firebase from "firebase";

const New_meet =() =>{
    
    var firebaseConfig={
        apiKey: ,
        authDomain: ,
        databaseURL: ,
        projectId: ,
        storageBucket: ,
        messagingSenderId: ,
        appId: ,
        measurementId: 
    };
    
    console.log("here");
    firebase.initializeApp(firebaseConfig);
    
    const preObject = document.getElementById('object');
    const dbRefObject = firebase.database().ref().child('object');
    dbRefObject.on('value',snap=>console.log(snap.val()));
    
    return(
        <div>
            <p id="object"></p>
        </div>
    );
}

export default New_meet;

The problem is, when I insert that firebaseInitializeApp function(already present for now), it gives the error

"Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app)."

And when I remove that line, it shows

 "Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app)"

I am not importing firebase in App.js or any other components. Have never used Firebase before so no clue how to tackle this.

6
  • 1
    try moving your firebaseConfig and initializeApp method outside/before the component Commented Sep 26, 2020 at 6:59
  • 1
    On every update, it's going to initialize, the firebase, but you have to do that only once, don't know the structure, but just like redux store we initialize that once, you have to do prevent that re-initialization. Commented Sep 26, 2020 at 7:01
  • @VaibhavRai Any ideas on how it's done? I have never used firebase before, and can only find android solutions online, not javascript. Commented Sep 26, 2020 at 7:14
  • @wdm that worked! although snap updates are not happening, but maybe that error is in my code. I will figure that out.Thanks a lot!!! Commented Sep 26, 2020 at 7:19
  • 1
    @VaibhavRai yeah that works, along with moving the config json and initialize method outside the component, as mentioned by wdm. Commented Sep 26, 2020 at 8:30

1 Answer 1

2

Seems like you're using a new instance of the firebase app every time the module reloads.

You have to initialize your firebase once and use the instance throughout your app.

Create a new file where you could initialize firebase and use it throughout the app.

firebase.js

import firebase from 'firebase';

try {
   firebase.initializeConfig({ /* Config here* /})
} catch(err) {
   // swallow the error
   console.log(err)
}

/* use this firebase in your project. */
export default firebase;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.