第二代Google Cloud Functions onDocumentDeleted事件无法返回文档对象
第二代Cloud Functions onDocumentDeleted事件异常,无法获取文档快照
我在部署第二代Google Cloud Functions的onDocumentDeleted函数时,删除Firestore文档后触发函数,获取到的event格式异常,且无法从中拿到文档快照。
我的函数代码
import type { QueryDocumentSnapshot } from 'firebase-admin/firestore'; import { FirestoreEvent, onDocumentDeleted, } from 'firebase-functions/v2/firestore'; export const myFunction = onDocumentDeleted( 'myDocuments/{myDocumentID}', async ( event: FirestoreEvent< QueryDocumentSnapshot | undefined, { myDocumentID: string } >, ): Promise<void> => { console.log('Event is:', JSON.stringify(event)); const snapshot = event.data as QueryDocumentSnapshot<MyDocument>; console.log('Snapshot is:', JSON.stringify(snapshot)); }, );
我的部署脚本
FUNCTION_NAME=my-function ENTRY_POINT=myFunction PROJECT_ID=my-project gcloud functions deploy ${FUNCTION_NAME} \ --gen2 \ --runtime=nodejs20 \ --region=europe-west3 \ --trigger-location=eur3 \ --source=./deploy \ --entry-point=${ENTRY_POINT} \ --project=${PROJECT_ID} \ --env-vars-file=.env.stage.yaml \ --trigger-event-filters=type=google.cloud.firestore.document.v1.deleted \ --trigger-event-filters=database='(default)' \ --trigger-event-filters-path-pattern=document='myDocuments/{myDocumentID}' \ --allow-unauthenticated \
执行结果
Event is: {"0":18,"1":151,"2":232,"3":1,"4":10,"5":60,"6":112,"7":114,"8":111,"9":106,"10":101,"11":99,"12":116,"13":115,"14":47,"15":110,"16":105,"17":114,"18":111,"19":45,"2...
Snapshot is: undefined
问题原因与修复方案
1. 事件格式异常的根源
你同时使用了函数SDK的触发器定义(onDocumentDeleted('myDocuments/{myDocumentID}'))和gcloud命令的触发器参数,两种定义方式冲突,导致事件结构解析错误,出现了类似{"0":18,...}的异常格式。第二代Cloud Functions中,触发器定义二选一即可。
2. 无法获取快照的原因
onDocumentDeleted事件默认不保留已删除的文档数据,必须显式启用数据保留配置,才能获取删除前的文档快照。
修复步骤一:统一触发器定义方式
移除gcloud命令中所有触发器相关参数,保留SDK中的触发器定义即可:
FUNCTION_NAME=my-function ENTRY_POINT=myFunction PROJECT_ID=my-project gcloud functions deploy ${FUNCTION_NAME} \ --gen2 \ --runtime=nodejs20 \ --region=europe-west3 \ --trigger-location=eur3 \ --source=./deploy \ --entry-point=${ENTRY_POINT} \ --project=${PROJECT_ID} \ --env-vars-file=.env.stage.yaml \ --allow-unauthenticated \
修复步骤二:启用数据保留获取删除前快照
修改函数代码,在触发器配置中添加dataRetention: true,并正确处理事件数据:
import type { QueryDocumentSnapshot } from 'firebase-admin/firestore'; import { FirestoreEvent, onDocumentDeleted, } from 'firebase-functions/v2/firestore'; // 定义你的文档结构 interface MyDocument { // 示例字段 name: string; value: number; } export const myFunction = onDocumentDeleted( { document: 'myDocuments/{myDocumentID}', // 启用数据保留,获取删除前的文档快照 dataRetention: true }, async ( event: FirestoreEvent< QueryDocumentSnapshot | undefined, { myDocumentID: string } >, ): Promise<void> => { // 现在event格式恢复正常 console.log('Event ID:', event.eventId); console.log('Deleted document ID:', event.params.myDocumentID); if (event.data) { const deletedData = event.data.data() as MyDocument; console.log('Deleted document data:', deletedData); } else { console.log('无删除数据可用(未启用数据保留或数据已过期)'); } }, );
内容的提问来源于stack exchange,提问作者Meros Debray
相关产品推荐
相关产品推荐

