I created a cloud run funciton to sync firestore to meilisearch. The function fired by google.cloud.firestore.document.v1.written
authenticates fine with firestore then tries to sync using ( part of the code):
module.exports.syncToMeilisearchMediaPOST = functions.firestore
.document("collectionNameHere/{docId}")
.onWrite(async (change, context) => {
console.log("Function triggered with config:", {
projectId: process.env.GCLOUDPROJECT,
functionName: context.eventId,
documentPath: collectionNameHere/${context.params.docId},
eventType: change.before.exists ? (change.after.exists ? 'update' : 'delete') : 'create'. //always false!
});
const docId = context.params.docId;
if (!change.before.exists) {
const newDocument = change.after.data();
console.log(CREATE Document in collectionNameHere/${docId}:, newDocument);
console.log("CREATE Payload being sent to Meilisearch:", JSON.stringify({ id: docId, ...newDocument }, null, 2));
console.log(CREATE Triggered by collectionNameHere/${context.params.docId});
console.log("CREATE Change After:", JSON.stringify(change.after, null, 2));
Some issues:
- The first issue that happens is that
change.before
andchange.after
are both false always, regardless of the operation. docId
is fine butnewDocument
isundefined
(most likely caused by the first issue).
CREATE Document in collectionNameHere/O91JIlRjeJLtACZ6awRF: _undefined
CREATE Payload being sent to Meilisearch: { “id”: “O91JIlRjeJLtACZ6awRF” }
CREATE Triggered by collectionNameHere/O91JIlRjeJLtACZ6awRF
My service account used for the cloud run has datastore.user
and firebaseadminSDK
roles.
The application is a webapp where users login and update the data I am after in the collection.
As a result of the above – only the document ID is being synced to Meilisearch. I need the whole document to be synced. Fetching the document by trying to read is failing with error 5 not found despite doing granting the service account proper roles.
Any ideas or tips?
From a quick check in the reference docs for DocumentSnapshot
it seems that exists
is a method, not a property. So you need to invoke it like this:
// 👇
if (!change.before.exists()) {
...
Without the ()
, you’re essentially checking if the method itself exists, which is always gonna be true.
1