1
votes

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?

1
Why do you need to use a cloud function?, What is preventing you from doing this within your application?Philip
I want to learn something new, Firestore seems really good and even better if I can extend it using cloud function. and maybe it will be great if I just write one code in the background than for both in ios and android app, less code in client sidesarah

1 Answers

1
votes

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.