Home > front end >  Initializing 2 firebase apps in python
Initializing 2 firebase apps in python

Time:09-03

As part of my project, I've made 2 different Firestore databases. Usually, when I work with 1, it sets up fine in my project file. Now that I'm implementing BOTH of them in my python file (running discord.py), it throws up an error! Here's the code for reference:

from firebase_admin import credentials, firestore, initialize_app

sccred = credentials.Certificate("scores-firebase.json")
scdefault_app = initialize_app(sccred)
scoredb = firestore.client(scdefault_app)
scdb = scoredb.collection('users')


stcred = credentials.Certificate("storage-firebase.json")
stdefault_app = initialize_app(stcred)
storagedb = firestore.client(stdefault_app)
stdb = storagedb.collection('january')

If I comment out the last 4 lines, it works fine, however, if I don't then the following error shows up:

Traceback (most recent call last):
  File "main.py", line 51, in <module>
    stdefault_app = initialize_app(stcred)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/firebase_admin/__init__.py", line 71, in initialize_app
    raise ValueError((
ValueError: The default Firebase app already exists. This means you called initialize_app() more than once without providing an app name as the second argument. In most cases you only need to call initialize_app() once. But if you do want to initialize multiple apps, pass a second argument to initialize_app() to give each app a unique name.

I understood I need to add another argument, and I even went over the official firebase-admin docs for the package, yet I don't know how to add the required argument as there is no mention of it in the documentation.

Here's the documentation I referred to: https://firebase.google.com/docs/reference/admin/python/firebase_admin.db

CodePudding user response:

You must specify a name for any Firebase Admin instances other than the default one:

# add the name param
stdefault_app = initialize_app(stcred, name='second_admin_instance')

Checkout the documentation for more information.

  • Related