Reputation: 3104
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
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