Guest User

Untitled

a guest
Jan 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. export const deletePhoto = functions.firestore
  2. .document("posts/{postId}")
  3. .onUpdate((change, context) => {
  4. // Get an object representing the document
  5. const updatedPost = change.after.data() as any;
  6.  
  7. // ...or the previous value before this update
  8. const oldPost = change.before.data() as any;
  9.  
  10. const oldImages: string[] = oldPost.images;
  11. const newImages: string[] = updatedPost.images;
  12.  
  13. const deletedImages = oldImages.filter(oldImage => {
  14. return !newImages.some(newImage => newImage === oldImage);
  15. });
  16.  
  17. const bucket = firebase.storage().bucket();
  18.  
  19. const imagesRemovePromises = deletedImages.map((imagePath: string) => {
  20. return bucket.file(imagePath).delete();
  21. });
  22.  
  23. return Promise.all(imagesRemovePromises);
  24. });
Add Comment
Please, Sign In to add comment