Divya Galla
Divya Galla

Reputation: 513

Google Cloud Firestore Triggers

How to add a new attribute for a newly created document by cloud function that was triggered by onCreate() cloud firestore trigger. Does one can use same approach to upadate a document in client side as well as to server side ie in Cloud Functions?

Upvotes: 0

Views: 940

Answers (1)

Mike McDonald
Mike McDonald

Reputation: 15953

Per the docs, you can use event.data.ref to perform operations:

exports.addUserProperty = functions.firestore
  .document('users/{userId}')
  .onCreate(event => {
    // Get an object representing the document
    // e.g. {'name': 'Marie', 'age': 66}
    var data = event.data.data();

    // add a new property to the user object, write it to Firestore
    return event.data.ref.update({
      "born": "Poland"
    });
});

Upvotes: 2

Related Questions