Skip to content

Instantly share code, notes, and snippets.

@shangyilim
Created January 20, 2019 14:26
Show Gist options
  • Save shangyilim/f7f1a27caf0825942c6eaba143f5a481 to your computer and use it in GitHub Desktop.
Save shangyilim/f7f1a27caf0825942c6eaba143f5a481 to your computer and use it in GitHub Desktop.
export const deletePhoto = functions.firestore
.document("posts/{postId}")
.onUpdate((change, context) => {
// Get an object representing the document
const updatedPost = change.after.data() as any;
// ...or the previous value before this update
const oldPost = change.before.data() as any;
const oldImages: string[] = oldPost.images;
const newImages: string[] = updatedPost.images;
const deletedImages = oldImages.filter(oldImage => {
return !newImages.some(newImage => newImage === oldImage);
});
const bucket = firebase.storage().bucket();
const imagesRemovePromises = deletedImages.map((imagePath: string) => {
return bucket.file(imagePath).delete();
});
return Promise.all(imagesRemovePromises);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment