3
votes

When writing firebase rules, you can access the request data via request.resource.data. This is useful because you can look at the nature of the request to determine its intent, its write target and permit or deny. This enables merging properties into an object within a document owned by a user, vs using a nested collection of documents.

I would like to access the same request data in the cloud function callbacks update/write/etc, but I don't see it, and I'm left to do an object compare with change.before and change.after. It's not a problem, but did I miss something in the documentation?

Per documentation: https://firebase.google.com/docs/firestore/extend-with-functions

exports.myFunctionName = functions.firestore.document('users/marie').onWrite((change, context) => {
   // ... the change or context objects do not contain the request data
});
1
Can you add your code or some very minimalistic example to make it clear? - mark922
We are also looking for the same thing, our use case is we want to know if the client sent a flag in the request or not. Details below: We want to trigger some functionality if the client sends a flag in the update, if there is no way to get the actual request, we will have to clear the flag after executing our logic, which will result in additional write cost. We could ofcourse have a cloud function separately and let the client call that function but we would like to avoid it as we will then need to care about fault tolerance that as well. - Tech Alpha Studios

1 Answers

0
votes

I had the exact same question when I realized that a function listening for updates was being triggered regardless of the property being updated, despite having a 'status' in data check. The catch that data represented handler.after.data. Although I wasn't able to access the request data, either from the handler or from the context, I was able to solve the problem by adding an additional check which serves the same purpose. Namely:

const dataBefore = handler.before.data();
const dataAfter = handler.after.data();

if (status in dataBefore && status in dataAfter) {
  if (dataBefore.status === 'unpublished' && dataAfter.status === 'published') {
    // handle update
  }
}