我尝试在 Cloud Run 服务器上运行以下代码:
cred = credentials.Certificate("firebaseCertificate.json")
firebase_admin.initialize_app(cred)
app = Flask(__name__)
@app.route('/something', methods=["POST"])
def some_route():
some_id = req["some_id"]
print(some_id)
# Nothing prints after here, nothing happens in the Firestore DB
db = firestore.client()
doc_ref = db.collection('some_collection').document(some_id)
doc_data = doc_ref.get().to_dict()
if doc_data == None:
doc_data = {}
doc_data['my_condition'] = True
doc_ref.set(doc_data)
print("done modifying changes to firestore")
第一次打印后(通常显示),终端上没有任何显示,没有错误,什么也没有。查看我的 Firestore DB 后,没有添加或修改任何文档。
这种情况发生在我的 Cloud Run 容器上以及我的本地计算机上。我使用 firebase_admin 在 Firestore 上制作文档时是否做错了什么?
您可能还认为我应该使用 Cloud Functions,对于这段代码,我应该使用 Cloud Functions,但是为了使全局代码正常工作,我不能,因为您需要使用自定义 docker 容器的外部依赖项安装。
编辑:现在它只给我一个超时错误。尝试从数据库获取数据或使用 doc_ref.set 设置数据时会发生错误。我有一个完全有效的(我测试过重置它)firebaseCertificate.json,所以我不明白出了什么问题。
此类代码已在另一个端点中使用并且运行良好。
您不会在函数末尾返回任何内容,Flask 应用程序需要一个路由返回值,用于创建对 HTTP 请求的响应。
一个简单的字符串就足够了。 请阅读此处了解更多详细信息。
@app.route('/something', methods=["POST"])
def some_route():
some_id = req["some_id"]
print(some_id)
# Nothing prints after here, nothing happens in the Firestore DB
db = firestore.client()
doc_ref = db.collection('some_collection').document(some_id)
doc_data = doc_ref.get().to_dict()
if doc_data == None:
doc_data = {}
doc_data['my_condition'] = True
doc_ref.set(doc_data)
return "done modifying changes to firestore"