Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

'event' is wrong 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:

export const myFunction = onDocumentDeleted(
  'myDocuments/{myDocumentID}',
  async (
    event: FirestoreEvent<
      QueryDocumentSnapshot | undefined,
      { flowID: string }
    >,
  ): Promise<void> => {
    console.log('Event is:', JSON.stringify(event));
    const snapshot = event.data as QueryDocumentSnapshot<Flow>;
    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 \

The result: 

Meros_0-1707748769925.png

 

0 1 145
1 REPLY 1

Hi @Meros,

Welcome to Google Cloud Community!

It seems that it has already had an answer from Stack Overflow. Here's the post from the community member:

The code you're writing is using the Firebase SDK. It can only be deployed using the Firebase CLI. You can't use gcloud to deploy functions that use the Firebase SDK.

If you want to deploy a function with gcloud, you must follow the instructions in the GCP documentation. If you want to deploy a function using a Firebase SDK, you must use the Firebase CLI. Also I think you will do better to follow the Firestore examples on a different page of Firebase docs.


Hope this helps.