1
votes

I just added Cloud Function functionality to my Firestore Project and I was able to follow the docs and get the hello world https endpoint call to work. My question now is how do I start actually using cloud functions to update data in my database? Currently my Firestore structure is as follows:

Users(Collection)->{user_unique_id}(document)->budget(collection)->[data/general](two different documents).

So I want a function to be called whenever either the data or general documents are updated that takes all of the data in both the data and general documents and runs some math, and then returns updated values for each document.

My question is, how do I actually do this? Right now I have tried functions.firebase.document("users/{userId}").onUpdate(), but this is not doing anything, and I am not sure how to get the currently used userId? On top of this, how could I make a function that I can just call regularly from a method in my app?

Thanks guys.

1

1 Answers

0
votes

The best practice is to use the Firestore triggered Cloud Function. You can visit the Cloud Firestore triggers documentation for more information. Choose the method onUpdate which is specified in Trigger a Cloud Firestore function section. Then you put the document's path and everytime this document will be updated, the function will be triggered and will perform some actions that you will specify in the code.

However, it is mentioned that "keep in mind Cloud Firestore is currently in beta which may result in unexpected behavior.", BETA versions are not suggested to be used in production.


how could I make a function that I can just call regularly from a method in my app?

Create an HTTP triggered Cloud Function and every time you want to call it, just make an HTTP request and the function will be triggered.


I am not sure how to get the currently used userId?

This was answered on a previous Stackoverflow question: Getting the user id from a Firestore Trigger in Cloud Functions for Firebase?

"Sorry the auth is not yet added in the Firestore SDK. We have it listed in the next features.

Keep an eye out on our release notes for any further updates."