谷歌云Firestore触发器无法检测到对数据库的写入操作

3

操作系统: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(并相应地部署它),结果也是一样的——没有任何迹象表明事件已发生。
如果您有任何提示/建议,那将会非常棒。谢谢!

我认为你在 --trigger-resource 中可能有一个错误,根据这个文档,默认值应该在括号中。这可能是你的触发器没有触发的原因,因为它找不到资源。 - rsalinas
@rsalinas 这个给了我 -bash: syntax error near unexpected token '(' - krt24
没关系,忘记使用单引号转义路径了。谢谢! - krt24
1个回答

2
我相信您需要以下部署命令,在其中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

尝试运行以上命令会提示 -bash: syntax error near unexpected token ('`。 - krt24
啊,不用在意,我忘记使用单引号转义路径了。谢谢! - krt24

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接