Firestore 触发的云函数参数的类型?

cbd*_*per 2 node.js firebase typescript google-cloud-functions google-cloud-firestore

以下是 Firestore 云功能的可用触发器:

https://firebase.google.com/docs/firestore/extend-with-functions

在创建

functions.firestore
.document('my-collection/{doc-id}')
.onCreate((snap, context) => { /* ... */ });
Run Code Online (Sandbox Code Playgroud)

删除时

functions.firestore
.document('my-collection/{doc-id}')
.onDelete((snap, context) => { /* ... */ });
Run Code Online (Sandbox Code Playgroud)

更新

functions.firestore
.document('my-collection/{doc-id}')
.onUpdate((change, context) => { /* ... */ });
Run Code Online (Sandbox Code Playgroud)

写时

functions.firestore
.document('my-collection/{doc-id}')
.onWrite((change, context) => { /* ... */ });
Run Code Online (Sandbox Code Playgroud)

我正在将我的项目转换为 Typescript。我应该为 paramschange context和使用什么类型snap

Ren*_*nec 6

以下是类型:

在创建:

snapshot: FirebaseFirestore.QueryDocumentSnapshot
context: functions.EventContext
Run Code Online (Sandbox Code Playgroud)

删除:

snapshot: FirebaseFirestore.QueryDocumentSnapshot
context: functions.EventContext
Run Code Online (Sandbox Code Playgroud)

更新:

change: functions.Change<FirebaseFirestore.QueryDocumentSnapshot>
context: functions.EventContext
Run Code Online (Sandbox Code Playgroud)

写时:

change: functions.Change<FirebaseFirestore.DocumentSnapshot>
context: functions.EventContext
Run Code Online (Sandbox Code Playgroud)

此处此处的文档中的更多详细信息。


如果您使用的是 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) => {
...
}

Run Code Online (Sandbox Code Playgroud)