Firestore Function - onWrite Event

Greetings, I hope that this post will help someone who runs across this problem. I’m trying to update the Algolia Index to Create/Update/Delete objects as things change in my Firestore Database. Unfortunately, I seem to be crashing my cloud function; if I comment out index.deleteObject(oldProduct); then my products will be created and updated in Algolia as events happen in Firestore. however, if I leave it in, then it just completely crashes the function. Let me know if any additional details are needed

Here’s my code:

exports.onProductCRUD = functions.firestore
.document("products/{productsId}")
.onWrite(event => {
  // Get the product document
  const product = event.data.data();
  if (event.data.previous) {
    var oldProduct = event.data.previous.data();
    oldProduct.objectID = event.params.oldProductId;
      }
  // Add an "objectID" field which Algolia requires
  product.objectID = event.params.productsId;

  // Update the Algolia Index
  const index = client.initIndex(ALGOLIA_INDEX_NAME);
  const saveResult = index.saveObject(product);

  // will delete object asynchronously while saving new product
  index.deleteObject(oldProduct);

  return saveResult;
});

Here’s the error from my function:

    Error: The data for "projects/test-ffdbb/databases/(default)/documents/products/9xB70WjVNrKuV4fWamkz" does not exist.
    at DocumentSnapshot.protoFields (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:337:13)
    at DocumentSnapshot.data (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:318:23)
    at exports.onProductCRUD.functions.firestore.document.onWrite.event (/user_code/index.js:17:30)
    at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27)
    at next (native)
    at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
    at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36)
    at /var/tmp/worker/worker.js:687:26
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)
1 Like

Hi @ovidioborrero, from the error message, it seems that the line exports.onProductCRUD = functions.firestore .document("products/{productsId}") or event.data.data()

is failing because the productsId at this particular moment does not exist in firebase. As I am very new to firebase I don’t exactly know what it means but maybe you could

The current error message does not says that Algolia failed but somehow maybe the delete inside Algolia is also impacting your own firebase datastore? Did you setup maybe a two way replication or something? Not trying to diminish this issue impact but I can’t extract any error from Algolia in your message, only Firebase is failing for now.

Some good info here too: https://firebase.google.com/docs/functions/firestore-events#limitations_and_guarantees

Let us know how it goes.

The problem here is the line:

const product = event.data.data();

Firebase functions for firestore triggers documentation is wrong.

You’re trying to access the data from an object that does not exists anymore.

You should use the parameter event.data.exists, wich is not documented anywhere.