我有以下代码
//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
云功能是否有办法触发删除任何一张图片?这里的区别是房间的图片更深一层,所以我认为pictureId
与roomId/picture
不匹配。
答案 0 :(得分:0)
Cloud Functions不了解JSON的含义。因此,删除galleries/{galleryId}/{pictureId}
的触发器实际上只是意味着只要触发galleries
下第二级的节点,就会触发此函数。
在您显示的结构中,这意味着只要删除/galleries/room/roomId
或/galleries/foods/picture
,此函数就会触发。当您从房间删除最后一张照片时,其中的第一个会触发。