Created
January 20, 2019 14:26
-
-
Save shangyilim/f7f1a27caf0825942c6eaba143f5a481 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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