Cloud Firestore Function Javascript: Triggering a function with whereEqualTo query

1857 views javascript
1

In Android we can listen inside a collection with simple queries/compound queries by setting a query like this.

mQuery = mDatabase.collection("Users")
            .document(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .collection("Request").whereEqualTo("status","received")
            .whereEqualTo("type","friend_request")
            .orderBy("time", Query.Direction.DESCENDING)
            .limit(5);

Then setting a listener based on that Query like this.

 mQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()){...

Can I do similar things with Cloud Function? I am currently using this line of code and it is working.

functions.firestore.document("Users/{user_id}/Notifications/{notification_id}").onCreate((added, context) => {

But for some changes, now I would like to trigger a function only where document field is equal to something then for loop I think?

Another thing is how am I going to get the document Id of 'Notification' collection now if queries is possible, now I use wild card then retrieving it like this.

const notification_id = context.params.notification_id;

answered question

1 Answer

0

With Cloud Functions triggers for Firestore, you can only specify which collections and documents cause the function to fire by using wildcards. There are no query filters available. If you want to further restrict which changed documents require some action, you have to check the contents of the document in your code.

posted this

Have an answer?

JD

Please login first before posting an answer.