部署带Firestore文档创建触发器的Cloud Function遇400错误
解决Gen2 Cloud Function Firestore触发器部署错误
问题原因
你遇到的错误是因为Gen2 Cloud Function的Firestore create事件触发器不支持用document作为路径模式的属性,正确的路径匹配属性应该是resource,同时必须指定要监听的Firestore数据库。
修正后的部署命令
将原命令中的路径模式参数替换为针对resource属性的完整路径,并添加数据库过滤条件:
gcloud functions deploy send-verification-mail ^ --gen2 ^ --runtime=python312 ^ --trigger-location=asia-south2 ^ --region=asia-south2 ^ --source=. ^ --entry-point=send_verification_mail ^ --set-env-vars GOOGLE_CLOUD_PROJECT=myproject ^ --trigger-event-filters=type=google.cloud.firestore.document.v1.create ^ --trigger-event-filters=database=(default) ^ --trigger-event-filters-path-pattern=resource=projects/myproject/databases/(default)/documents/users/{username}
关键修正点
- 替换
document为resource:Gen2触发器要求路径匹配针对完整的资源路径,格式为projects/[项目ID]/databases/[数据库ID]/documents/[文档路径],默认数据库ID为(default)。 - 添加数据库过滤:必须通过
--trigger-event-filters=database=(default)指定要监听的Firestore数据库,否则触发器无法正确绑定到目标数据库。
内容的提问来源于stack exchange,提问作者Tanav Gupta
相关产品推荐
相关产品推荐

