Reputation: 75
when I was trying to connect google firebase real time database, I got this error:
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.
Here is my code:
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
cred = credentials.Certificate('firebase-sdk.json')
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://test-139a6-default-rtdb.firebaseio.com/'
})
Upvotes: 5
Views: 6418
Reputation: 50830
You need to initialize the Admin SDK only once. You can check if the Admin SDK is already initialized using this if statement:
if not firebase_admin._apps:
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://test-139a6-default-rtdb.firebaseio.com/'
})
Upvotes: 4
Reputation: 15537
You only need to initialize (create) the app once. When you have created the app, use get_app
instead:
# The default app's name is "[DEFAULT]"
firebase_admin.get_app(name='[DEFAULT]')
Upvotes: 4