firestore cloud functions onCreate/onDelete sometimes immediately triggered twice

后端 未结 3 1153
再見小時候
再見小時候 2020-12-05 16:27

I have observed this behavior occasionally with both onCreate and onDelete triggers.

Both the executions happened for the same document created in firestore. There

3条回答
  •  一整个雨季
    2020-12-05 17:13

    Based on @saranpol's answer we use the below for now. We have yet to check if we actually get any duplicate event ids though.

    const alreadyTriggered = eventId => {
      // Firestore doesn't support forward slash in ids and the eventId often has it
      const validEventId = eventId.replace('/', '')
    
      const firestore = firebase.firestore()
      return firestore.runTransaction(async transaction => {
        const ref = firestore.doc(`eventIds/${validEventId}`)
        const doc = await transaction.get(ref)
        if (doc.exists) {
          console.error(`Already triggered function for event: ${validEventId}`)
          return true
        } else {
          transaction.set(ref, {})
          return false
        }
      })
    }
    
    // Usage
    if (await alreadyTriggered(context.eventId)) {
      return
    }
    

提交回复
热议问题