algolia / firestore-algolia-search

Apache License 2.0
108 stars 33 forks source link

Firebase Transform Function not removing unwanted key from a field object #199

Closed WebDevMehta closed 3 months ago

WebDevMehta commented 4 months ago

My Cloud Function working fine in tesing. But when I link it with algolia for data transformation. It's not work. Before Indexing any document. This function should be called then it should check if “reports” key present inside “view”. If it is then “reports” key must be deleted.

My Firebase Document:

{
    "created": FirestoreTimestamp,
    "cost": Number,
    "status": Number,
    "view": {
        "material": string,
        "tests": Array of strings,
        "reports": Array of Objects,
    }
}
import {onRequest} from "firebase-functions/v2/https";

exports.transformLabReport = onRequest((req: any, res: any) => {
  const originalDocument = req.body.data;
  let transformedDocument = {...originalDocument};
  if ("view" in transformedDocument) {
    if ("reports" in transformedDocument["view"]) {
      transformedDocument = {
        ...transformedDocument,
        view: {
          ...transformedDocument.view,
          reports: undefined,
        },
      };
    }
  }
  res.status(200).send({data: transformedDocument});
});

After contacting Algolia Support they suggested I use the transform function same way its shown in example. So I modified it to

import * as functions from "firebase-functions/v1";

const removeReportsData = (payload: any) => {
  return {
    ...payload,
    "view": {
      ...payload.view,
      "reports": null,
    },
  };
};

exports.transformLabReport = functions.https.onCall((payload) => {
  const transformedData = removeReportsData(payload);
  return transformedData;
});

But it still didn't work. So they told me to open an issue.

smomin commented 4 months ago

Hey @WebDevMehta are you using the TransformFunction configuration in the extension configuration screen?

https://github.com/algolia/firestore-algolia-search

Transform Function Name (experimental): What is the Firebase Cloud Function Name? This is the name of the Firestore Cloud Function for transforming the data before transmitting to Algolia for indexing. This function should be deployed to the same Firebase Project and Location as the Firestore/Algolia extension. Refer to [Call functions for your app](https://firebase.google.com/docs/functions/callable). Below is an example of a Transform function used for my testing:

  import * as functions from "firebase-functions";

  const doStuffToData = (payload: any) => {
    return {
    ...payload,
    "hello": "world",
    };
  };

  export const helloWorld = functions.https.onCall((payload) => {
    const transformedData = doStuffToData(payload);
    return transformedData;
  });
Note: The Transform Firebase Function should be set up to unauthenticated users at this time.
WebDevMehta commented 3 months ago

I found the issue. My Transform Cloud Function and Algolia Index Functions were in the different regions. After redeploying them in the same region its working perfectly fine. Thanks a lot.