Why I get this error on python firebase-admin initialize_app()?
Asked Answered
Q

2

5

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/'
})
Quesada answered 18/7, 2021 at 7:17 Comment(0)
G
4

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]')
Gilbert answered 18/7, 2021 at 7:46 Comment(2)
Thank you for reply André, but I wonder, how can I see all of my firebase app on enviroment?Quesada
Combining this answer and @Dharmaraj 's solved my problem.Bomar
A
4

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/'
    })
Apollyon answered 18/7, 2021 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.