‘event’ is not returning an object representing the document for ‘onDocumentDeleted’ function in Google Cloud Functions 2nd gen

I am trying to deploy a onDocumentDeleted Google Cloud Function (2nd gen) but when I delete a document in my application, the event I get is not correct and I can’t get the snapshot from it.

My function:

import type { QueryDocumentSnapshot } from 'firebase-admin/firestore';
import {
    FirestoreEvent,
    onDocumentDeleted,
} from 'firebase-functions/v2/firestore';

export const myFunction = onDocumentDeleted(
  'myDocuments/{myDocumentID}',
  async (
    event: FirestoreEvent<
      QueryDocumentSnapshot | undefined,
      { myDocumentID: string }
    >,
  ): Promise<void> => {
    console.log('Event is:', JSON.stringify(event));
    const snapshot = event.data as QueryDocumentSnapshot<MyDocument>;
    console.log('Snapshot is:', JSON.stringify(snapshot));
  },
);

My deploy script:

FUNCTION_NAME=my-function
ENTRY_POINT=myFunction
PROJECT_ID=my-project

gcloud functions deploy ${FUNCTION_NAME} \
--gen2 \
--runtime=nodejs20 \
--region=europe-west3 \
--trigger-location=eur3 \
--source=./deploy \
--entry-point=${ENTRY_POINT} \
--project=${PROJECT_ID} \
--env-vars-file=.env.stage.yaml \
--trigger-event-filters=type=google.cloud.firestore.document.v1.deleted \
--trigger-event-filters=database="(default)" \
--trigger-event-filters-path-pattern=document="myDocuments/{myDocumentID}" \
--allow-unauthenticated \

Result:

Event is: {“0″:18,”1″:151,”2″:232,”3″:1,”4″:10,”5″:60,”6″:112,”7″:114,”8″:111,”9″:106,”10″:101,”11″:99,”12″:116,”13″:115,”14″:47,”15″:110,”16″:105,”17″:114,”18″:111,”19”:45,”2…

Snapshot is: undefined

  • On Stack Overflow, please don’t show pictures of text and code. Copy the text into the question itself and format it so that it’s easy to read, copy, and search. You can edit the question to correct this using the edit link at the bottom.

    – 

  • What is onDocumentDeleted? Please edit the question to show the entire code sample that doesn’t work. We should be able to copy what you have and run it for ourselves. I don’t see that from the examples in the documentation.

    – 




  • It’s the Cloud Firestore function trigger, I have edited the question 🙂

    – 




Leave a Comment