Jojo Narte
Jojo Narte

Reputation: 3104

Creating a Firestore document with a reference in cloud functions on update from updated item

I have this function that listens to updates on Firestore Document via functions.firestore.document().onUpdate().

Here is a snippet of how I'd like to approach it:

exports.createItem = functions.firestore
  .document('list/{itemId}')
  .onUpdate((change, context) => {
    const ref = REFERENCE_TO_DOCUMENT_CHANGED; // how to do this?
    const changedData = change.after.data();
    const changedValueId = changedData.id;
    const create = admin.firestore().doc(`newlist/${changedValueId}`);
    create.set({
      item: ref,
      ...changedData
    });
  });

Upvotes: 1

Views: 1385

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598916

From the Cloud Functions documentation on Firestore onUpdate trigger it seems that the updated document is available in change.after. The DocumentReference for that document is thus available as change.after.ref (or change.before.ref, since change.before and change.after refer to different snapshots of the same document).

Upvotes: 3

Related Questions