firebase cloud functions trigger on new documents and not document update

34 Views Asked by At

I want to process new documents and with cloud functions and here is a snippet of the code:

import { log } from "firebase-functions/logger";
import { onDocumentCreated } from "firebase-functions/v2/firestore";

export const test = onDocumentCreated("users/{userId}/jobs/", async (event) => {
  const data = event.data?.data();
  const id = event.data?.id;

  log(`${id} has been created with data: ${data}`);
});

This functions is being triggered even when a field is added or removed in an already existing document in the collection jobs. My goal is to only process the document when it is initially created not when something inside the document change.

I have also tried following paths and got the same result:

users/{userId}/jobs

users/{userId}/jobs/{jobId}

Environment

I'm using firebase emulator and adding the data manually in the emulator UI. I started the project with firebase init and only edited the index file and changed to the code provided above. node 21 and 18 is installed on the machine. 18 is being used to run the function.

Dependencies

"firebase-admin": "^11.8.0",

"firebase-functions": "^4.3.1"

0

There are 0 best solutions below