skip to Main Content

I have an Firebase Cloud Functions codebase that uses Firestore database. Everything below works when I use it in local emulator using firebase emulators:start but when I need to deploy it to Firebase I got below error:

Error: User code failed to load. Cannot determine backend specification

main.py

import json
from firebase_functions import https_fn
from firebase_admin import initialize_app, firestore
import flask
from enum import Enum
from flask import g
from endpoints.moon_phase import moon_phase_bp

# Initialize Firebase app and Firestore
initialize_app()
db = firestore.client()
app = flask.Flask(__name__)


# Set up a before_request function to make db available in blueprints
@app.before_request
def before_request():
    # g.db = db
    print("before_request")

app.register_blueprint(moon_phase_bp) //Doesn't even use db, but in the future it will.


# Firebase Function to handle requests
@https_fn.on_request()
def astro(req: https_fn.Request) -> https_fn.Response:
    with app.request_context(req.environ):
        return app.full_dispatch_request()

If I update the db initialisation to db = firestore.client it deploys, but obviously, it’s a function reference, so I cannot use Firestore db in my endpoints. This also means it’s not related to my Firebase credentials or project setup.

What might be the issue here?

2

Answers


  1. It seems like you intialized firestore correctly, i belive you have installed all needed dependenvies and librarys.

    1. Make sure your firebase setup is setup correctly and the service account has the right permissions
    2. If it runs locally but not on deployment check the environment variables if they may differ
    3. Debugging logs check the firebase logs for more detailed error messages, you can do that via the Firebase consle (helps a lot) there you’ll probably find the error message helping you advance in your project
    Login or Signup to reply.
  2. Have you checked your python set-up again and ensured that you have a requirements.txt file?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search