question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Uncaught (in promise): Firebase already initialized

See original GitHub issue

I’m running firebase.init() in the first major component that loads. This component checks if the user is logged in and redirects appropriately - all good till this point.

Now if I “back” out of the app (Android device used for testing), and try to launch the app again, I get the error “Error: Uncaught (in promise): Firebase already initialized”.

Is there a way to resolve this? Maybe by destroying the firebase instance when the user exits the app? Or check if firebase is initialized and reuse the existing instance?

Thanks!

My package.json

"nativescript-plugin-firebase": "^6.1.1",
"nativescript-angular": "6.0.0",
"nativescript-angular-cli": "0.1.9",
"tns-core-modules": "4.2.0-2018-06-29-01",

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
edusperonicommented, Feb 19, 2019

@mosesweb I think you @ the wrong person 😛

To shed a light into this issue, it does seem that firebase keeps running (and initialized) after all of angular dies. This IS an issue as it may not bind the new event listeners if you call them on init (your this WILL NOT point to the current instance).

Turns out we never faced it before because I’ve always used the “addcallback” methods, like addOnMessageReceivedCallback, so we replace the callback before it’s called by firebase. Perhaps @EddyVerbruggen can point to the correct approach to handling this? I couldn’t find a way to “remove” the callbacks, or a way to check if firebase is initialized or not.

It also seems like ngOnDestroy is not even called after the app is closed via back button, so the best approach is probably a check for when firebase is already initialized and a quick replacement of the callbacks.

Maybe using WeakRef may also work on determining if firebase should call the callbacks or store the events until they are rebound

Edit:

On a sidenote, nativescript-angular does not call ngOnDestroy when the app dies, but will soon (https://github.com/NativeScript/nativescript-angular/pull/1728) so we could remove the firebase callbacks when it’s destroyed and add them again when needed. Otherwise we might need to implement our own global handler which queues events for when angular is ready to receive them.

0reactions
moseswebcommented, Feb 15, 2019

@edusperoni thanks for the links. I did this, seems to work great. Just have a property you set if you should init firebase.


export class AppComponent implements OnInit { 

  shouldFireBaseInit: boolean = true;
  constructor()
  {
    
  }
ngOnInit(): void {
    //Called after the constructor, initializing input properties, and the first call to ngOnChanges.
    //Add 'implements OnInit' to the class.
    // Subscribe to begin listening for async result
    
  
applicationOn(launchEvent, (args: ApplicationEventData) => {
  if (args.android) {
      // For Android applications, args.android is an android.content.Intent class.
      console.log("Launched Android application with the following intent: " + args.android + ".");
  } else if (args.ios !== undefined) {
      // For iOS applications, args.ios is NSDictionary (launchOptions).
      console.log("Launched iOS application with options: " + args.ios);
  }
});

applicationOn(suspendEvent, (args: ApplicationEventData) => {
  if (args.android) {
      // For Android applications, args.android is an android activity class.
      console.log("SUSPEND Activity: " + args.android);
  } else if (args.ios) {
      // For iOS applications, args.ios is UIApplication.
      console.log("UIApplication: " + args.ios);
  }
});

applicationOn(resumeEvent, (args: ApplicationEventData) => {
  if (args.android) {
      // For Android applications, args.android is an android activity class.
      console.log("RESUMEVENT Activity: " + args.android);
      // set this to false
      this.shouldFireBaseInit = false;
  } else if (args.ios) {
      // probably add it here as well for ios
      // For iOS applications, args.ios is UIApplication.
      console.log("UIApplication: " + args.ios);
  }
});
  if(this.shouldFireBaseInit)
  {
    firebase.init({
      onAuthStateChanged: (data)  => { // optional but useful to immediately re-logon the user when he re-visits your app
      console.log(data.loggedIn ? "Logged in to firebase" : "Logged out from firebase");
      if (data.loggedIn) {
        console.log('logged in stuff');
      }
      else
      {
          console.log('not logged in');
      }
      }
    });
}
  }
  
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

NativeScript Firebase already initialized - Stack Overflow
init error: Firebase already initialized". This happens when the changes don't trigger a whole application restart (ex. an html file). It ...
Read more >
Fixing The 'FirebaseApp name already exists!' Error - Medium
Essentially, it is checking if there is already a firebase app running and only initializing if there isn't one. This works if you're...
Read more >
Admin Authentication API Errors | Firebase - Google
The credential used to initialize the Admin SDK has insufficient permission to access the requested Authentication resource. Refer to Set up a Firebase...
Read more >
error: the default firebase app already exists. this means you ...
error: the default firebase app already exists. this means you called initializeapp() more than once without providing an app name as the second...
Read more >
[Solved] No Firebase App '[DEFAULT]' has been created Error
While using Firebase services in Flutter, you may get ʺNo Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()ʺ Error in Flutter.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found