I have observed this behavior occasionally with both onCreate and onDelete triggers.
Both the executions happened for the same document created in firestore. There's only one document there so I don't understand how it could trigger the handler twice. the handler itself is very simple:
module.exports = functions.firestore.document('notes/{noteId}').onCreate((event) => {
const db = admin.firestore();
const params = event.params;
const data = event.data.data();
// empty
});
this doesn't happen all the time. What am I missing?
In my case I try to use eventId and transaction to prevent onCreate sometimes triggered twice
(you may need to save eventId in list and check if it exist if your function actually triggered often)
See the Cloud Firestore Triggers Limitations and Guarantees:
There is a Firecast video with tips for implementing idempotence.
Also two Google Blog posts: the first, the second.
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.