"initialize_app 'The default Firebase app already exists." Cloud functions pub sub

Viewed 3929

I'm writing a a pub sub function on Google Cloud (cloud functions inline editor) that will trigger every half hour and it uses firestore. For some reason, the function will trigger fine the first time it runs, but after that the following error keeps popping up:

in initialize_app 'The default Firebase app already exists. This means you 
called ' 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've had this error before when I used two apps, but this function is ONLY USING one firebase app. Here is the portion of my code where I suspect this is the problem:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

def hello_pubsub(event, context):
    pubsub_message = base64.b64decode(event['data']).decode('utf-8')
    fire = {<My firebase credentials>}
    cred = credentials.Certificate(fire)
    default_app = firebase_admin.initialize_app(cred)
    db = firestore.client()
    ........

I figured that the problem is exactly what the error says, I haven't declared the name for the app, so I tried this(along with the other attempt):

default_app = firebase_admin.initialize_app(cred,'App')
# other attempt
default_app = firebase_admin.initialize_app()

And this still does not work. Again, this works the first time the function is triggered, but after that it continually crashes.

Any suggestions?

Thanks for the help!

2 Answers

Since this is a cloud-function, you don't need to use credentials, the function will pick up the credentials from the environment. I'd suggest to change your function for this:

import firebase_admin
from firebase_admin import firestore

firebase_admin.initialize_app()
db = firestore.client()


def hello_pubsub(event, context):
    pubsub_message = base64.b64decode(event['data']).decode('utf-8')
    # Do your things

Use credentials when working with your functions before deployment and then remove the credentials part when deploying as you don't need it.

Also if you don't need firebase_admin for other than importing firestore, you can skip initializing the firebase_app and use firestore alone like this:

import base64

from google.cloud import firestore

db = firestore.Client()


def hello_pubsub(event, context):
    pubsub_message = base64.b64decode(event['data']).decode('utf-8')
    # Do your things

Note the difference between Firebase firestore client and google-cloud firestore client is a "Capital C" and that you have to install the firestore python library in your machine for developing and testing and modify your requirements.txt

If its a cloud-function the marked solution will have the same error, because you can run multiple instances of the same cloud-function in the same time, which means you app will already be initialized.

You might wanna used this:

from firebase_admin import initialize_app, delete_app, get_app

try:
    default_app = get_app()
except ValueError:
    default_app = initialize_app()

your code here

try:
    delete_app(default_app)
except ValueError:
    pass

get_app() will try to get name=_DEFAULT_APP_NAME and return back An App instance with the given name

delete_app() well it will gracefully delete an App instance

Related