Skip to content

event.data undefined in onDocumentWritten when migrating from V1 to V2 API #1659

Closed
@tharropoulos

Description

@tharropoulos

Related issues

#1464, but we measured the size of the responses and it does not exceed the 10MB threshold discussed in that issue.

[REQUIRED] Version info

node: v18.20.4

firebase-functions: 6.1.1

firebase-tools: 13.28.0

firebase-admin: 13.0.1

[REQUIRED] Test case

typesense/firestore-typesense-search#98

[REQUIRED] Steps to reproduce

When migrating from Firebase Functions v1 to v2 API, specifically from functions.firestore.document().onWrite() to onDocumentWritten(), the event.data property is coming back as undefined.

The existing V1 implementation of our backfill function:

const functions = require("firebase-functions");

const validateBackfillRun = (snapshot) => {
  if (![true, "true"].includes(snapshot.after.get("trigger"))) {
    functions.logger.error("Skipping backfill. `trigger: true` key was not found");
    return false;
  }

  const collectionsToSync = snapshot.after.get("firestore_collections");
  if (Array.isArray(collectionsToSync) && !collectionsToSync.includes(config.firestoreCollectionPath)) {
    functions.logger.error("Collection not in firestore_collections array");
    return false;
  }

  return true;
};

module.exports = functions.firestore.document(triggerPath).onWrite(async (snapshot, context) => {
  if (!validateBackfillRun(snapshot)) {
    return false;
  }
  // Rest of the function
});

Migrating over to the V2 API:

const {onDocumentWritten} = require("firebase-functions/v2/firestore");
const {error} = require("firebase-functions/logger");

const validateBackfillRun = (snapshot) => {
  if (![true, "true"].includes(snapshot.after.get("trigger"))) {
    error("Skipping backfill. `trigger: true` key was not found");
    return false;
  }

  const collectionsToSync = snapshot.after.get("firestore_collections");
  if (Array.isArray(collectionsToSync) && !collectionsToSync.includes(config.firestoreCollectionPath)) {
    error("Collection not in firestore_collections array");
    return false;
  }

  return true;
};

module.exports = onDocumentWritten(triggerPath, async (event) => {
  // event.data is undefined here
  if (!validateBackfillRun(event.data)) {
    return false;
  }
  // Rest of the function
});

Will result in functionality breaking, as event.data comes back as undefined.

Here's the commit and diff in question.

[REQUIRED] Expected behavior

Returning the data object

[REQUIRED] Actual behavior

event.data is undefined.

Were you able to successfully deploy your functions?

Did not deploy, caught in CI. The PR posted includes both a description of the changes and the test failure.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions