Rafael Honda
Rafael Honda

Reputation: 227

cloud function onUpdate inside document

it's possible to trigger onUpdate only on a document field?, I want to check if there's changes on the images field, that is an array, if yes, how I write the document path

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

enter image description here

Upvotes: 0

Views: 201

Answers (2)

IsaBostan
IsaBostan

Reputation: 60

CloudFirebaseFireStore -> trigger email on update works when you change state to RETRY.

Sample Flutter Code:

`
DocumentReference usersRef = FirebaseFirestore.instance
      .collection("mail")
      .doc("docID");
  usersRef.get().then((docSnap) {
    if (docSnap.exists) {
      
      usersRef.update({
        "delivery": {
          "state": "RETRY",
        },
        "to": [emailAddress],
        "message": {
          "html":
              """<html><body><p>Hello user info updated</p>
                  <p>Bla bla bla</p></body></html>""",
          "subject": "update User",
          "text": "Update"
        }
      });
    } else {
      usersRef.set({
        "to": [emailAddress],
        "message": {
          "html":
              """<html><body><p>Hello, new user created</p></body></html>""",
          "subject": "Create user",
          "text": "Create User"
        }
      });
    }
  });`

I have tried on FirebaseFireStore, when it updates it sends email again, changing delivery state to RETRY, so no need to write one more doc to firebasefirestore

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317467

onUpdate triggers will fire when anything inside a matched document changes. It doesn't matter what changed.

It's not possible to narrow a trigger down to a particular field. Triggers can only match an entire document. If you want to know if something changed in a field, you will need to write code to compare the "before" and "after" snapshots.

Upvotes: 1

Related Questions