Joã*_*tor 6 idempotent firebase google-cloud-functions google-cloud-firestore
我正在使用带有云功能的测试版Firestore.在我的应用程序中,我需要触发一个侦听onCreate事件/company/{id}/point/{id}并执行插入的函数(collection('event').add({...}))
我的问题是:使用Firestore的云功能需要幂等功能.我不知道如何确保如果我的函数连续两次使用相同的事件触发,我将不会添加两个具有相同数据的文档.
我发现context.eventId可以解决这个问题,但我不认识一种使用它的方法.
exports.creatingEvents = functions.firestore
.document('/companies/{companyId}/points/{pointId}')
.onCreate((snap, context) => {
//some logic...
return db.doc(`eventlog/${context.params.companyId}`).collection('events').add(data)
})
Run Code Online (Sandbox Code Playgroud)
两件事情:
context.eventId.如果存在,则在该功能中不执行任何操作.context.eventId属性的值放入您添加的文档中.这应该可以防止多次调用函数为给定的事件id添加多个文档.
| 归档时间: |
|
| 查看次数: |
610 次 |
| 最近记录: |