因此,以下是Firestore云函数的可用触发器:
https://firebase.google.com/docs/firestore/extend-with-functions
onCreate
functions.firestore
.document('my-collection/{doc-id}')
.onCreate((snap, context) => { /* ... */ });
on删除
functions.firestore
.document('my-collection/{doc-id}')
.onDelete((snap, context) => { /* ... */ });
onUpdate
functions.firestore
.document('my-collection/{doc-id}')
.onUpdate((change, context) => { /* ... */ });
onWrite
functions.firestore
.document('my-collection/{doc-id}')
.onWrite((change, context) => { /* ... */ });
我正在将我的项目转换为Typescript。参数change
、context
和snap
应该使用什么类型?
以下是类型:
onCreate:
snapshot: FirebaseFirestore.QueryDocumentSnapshot
context: functions.EventContext
on删除:
snapshot: FirebaseFirestore.QueryDocumentSnapshot
context: functions.EventContext
onUpdate:
change: functions.Change<FirebaseFirestore.QueryDocumentSnapshot>
context: functions.EventContext
onWrite:
change: functions.Change<FirebaseFirestore.DocumentSnapshot>
context: functions.EventContext
此处和文档中的更多详细信息。
如果你使用Typescript,你会导入/使用如下:
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
...
export const fnSomeCollectionTriggerOnUpdate =
functions.firestore.document('somecollection/{docId}')
.onUpdate(async (change: functions.Change<admin.firestore.QueryDocumentSnapshot>,
context: functions.EventContext) => {
...
}