Agung
Agung

Reputation: 13813

how do I get the dataID when using cloud firestore triggers function?

I have an Event Collection in the Firestore database like this:

enter image description here

I want to use cloud firestore triggers. when a user attends an event, the capacity of the event will be -1, and when this field is updated I want to automatically update another field ("rankPoint") +3

enter image description here

to implement this, I need to Trigger a function when a document is updated

from firestore documentation, it will be like this

exports.updateUser = functions.firestore
    .document('users/{userId}')
    .onUpdate((change, context) => {

      // Get an object representing the document
      // e.g. {'name': 'Marie', 'age': 66}
      const newValue = change.after.data();

      // ...or the previous value before this update
      const previousValue = change.before.data();

      // access a particular field as you would any JS property
      const name = newValue.name;

      // perform desired operations ...
    });

for my case, it should be 'events/{eventId}' right? but how do I get that eventID in the wildcard? does it come from client side? I mean in iOS/Android I will write the code to update like

db.collection("eventss").document("someEventIDHere").setData(data)

is it from the client?

Upvotes: 1

Views: 556

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317487

Your function will only be delivered the document that matched your function's pattern (users/{userId}) that was changed. Other documents are not available until you query for them. So, if you want a document from you events collection, you will have to write some code to access it, then decide what to do from there.

It sounds like you're expecting there to be an eventId wildcard, but there is not. There is just the userId wildcard that you defined in your function. Other values will need to be derived from the data you have available in your user document.

Upvotes: 1

Related Questions