Firebase Cloud Function onDelete可变参数长度

时间:2018-11-12 11:27:25

标签: node.js firebase firebase-realtime-database google-cloud-functions

我有以下代码

//index.js
export const deletePicture = functions.region("europe-west1").database
  .ref("galleries/{galleryId}/{pictureId}")
  .onDelete(pictures.deletePicture)

//pictures.js
export const deletePicture = (snap, {params: {galleryId}}) => {
  console.log("Image deletion detected in the database, deleting images 
  from the Storage...")
  const {fileName} = snap.val()
  const bucket = storage.bucket()
  const baseURL = `galleries/${galleryId}`
  // Thumbnails
  const promises = [sizes
    .map(size => 
      bucket.file(`${baseURL}/thumb_${size}_${fileName}`).delete())]
  // Original
  promises.push(bucket.file(`${baseURL}/${fileName}`).delete())

  return Promise
    .all(promises)
    .then(() => console.log(`All versions of ${fileName} are now deleted.`))
}

在我的实时数据库和存储中,我具有以下结构:

galleries
|_rooms
| |_roomId
|   |_picture
|_foods
  |_picture

上述onDelete云功能是否有办法触发删除任何一张图片?这里的区别是房间的图片更深一层,所以我认为pictureIdroomId/picture不匹配。

1 个答案:

答案 0 :(得分:0)

Cloud Functions不了解JSON的含义。因此,删除galleries/{galleryId}/{pictureId}的触发器实际上只是意味着只要触发galleries下第二级的节点,就会触发此函数。

在您显示的结构中,这意味着只要删除/galleries/room/roomId/galleries/foods/picture,此函数就会触发。当您从房间删除最后一张照片时,其中的第一个会触发。