gpt4 book ai didi

python - Google Cloud Firestore 触发器未检测到对数据库的写入

转载 作者:行者123 更新时间:2023-12-05 05:06:42 31 4
gpt4 key购买 nike

操作系统:Mac OS Catalina v 10.15.1

Python版本:Python 3.7.1

我正在用 Python 编写一个由写入/更新/等触发的云函数。到 Firestore 数据库。我的函数在我的 main.py 中基本上看起来像这样:

def hello_firestore(data, context):
""" Triggered by a change to a Firestore document.
Args:
data (dict): The event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
print("Hello")

我希望此函数监视文档 test_collection/test_document 的更新。我使用以下方法部署了这个函数:

gcloud functions deploy hello_firestore \
--runtime python37 --trigger-event providers/cloud.firestore/eventTypes/document.write \
--trigger-resource projects/PROJECT_ID/databases/default/documents/test_collection/test_document

函数部署成功,并出现在我的 Firebase 控制台和 GCP 控制台中。我在本地机器上运行了一个脚本:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import time

cred = credentials.Certificate("./path/to/adminsdk.json")
firebase_admin.initialize_app(cred)
db = firestore.client()

doc_ref = db.collection(u'test_collection').document(u'test_document')
doc_ref.set({u'test_field':'test_value'})

time.sleep(60)

doc_ref.update({u'test_field':'new_value'})

数据库相应更新。我希望函数执行(因为满足触发条件),并且我希望在 Firestore 控制台日志或 GCP 控制台日志中看到相应的 stdout 输出。但是,那里什么也没有——没有错误消息,也没有触发事件发生的迹象(唯一的日志是部署函数后产生的日志)。此外,在 Firestore 控制台中手动编辑数据库没有任何效果。

我也尝试过在 Node.js 中编写这个 Cloud Function(并相应地部署它),结果是一样的——没有迹象表明事件已经发生。

任何提示/建议都会很棒。谢谢!

最佳答案

我相信您需要以下部署命令,其中 default 位于括号中:

gcloud functions deploy hello_firestore \
--runtime python37 --trigger-event providers/cloud.firestore/eventTypes/document.write \
--trigger-resource projects/PROJECT_ID/databases/(default)/documents/test_collection/test_document

查看此处了解更多详情:https://cloud.google.com/functions/docs/calling/cloud-firestore

关于python - Google Cloud Firestore 触发器未检测到对数据库的写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59677287/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com