Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tharropoulos opened this issue Jan 7, 2025 · 0 comments

Comments

@tharropoulos
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants