【问题标题】:Google Cloud Firestore Triggers not Detecting Write to DatabaseGoogle Cloud Firestore 触发器未检测到写入数据库
【发布时间】:2020-04-27 20:16:12
【问题描述】:

操作系统: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 中编写这个云函数(并相应地部署它),结果是一样的——没有迹象表明事件已经发生。

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

【问题讨论】:

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

标签: python firebase google-cloud-platform google-cloud-firestore google-cloud-functions


【解决方案1】:

我相信您想要以下部署命令,其中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 ('`
  • 啊,没关系,忘记使用单引号转义路径。谢谢!
猜你喜欢
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-27
  • 2021-11-09
  • 2019-10-12
  • 1970-01-01
相关资源
最近更新 更多