我有一个使用 Firestore 数据库的 Firebase Cloud Functions 代码库。当我使用
firebase emulators:start
在本地模拟器中使用它时,下面的所有内容都有效,但是当我需要将其部署到 Firebase 时,我收到以下错误:
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()
如果我将数据库初始化更新为
db = firestore.client
,它就会部署,但显然,它是一个函数引用,因此我无法在端点中使用 Firestore 数据库。这也意味着它与我的 Firebase 凭据或项目设置无关。
这里可能出现什么问题?
看来您正确初始化了 firestore,我相信您已经安装了所有需要的依赖项和库。