Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 710
Landon G Avatar asked Mar 07 '26 15:03

Landon G


1 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

like image 139
Guanaco Devs Avatar answered Mar 10 '26 03:03

Guanaco Devs