如何从 Firebase 云存储中删除图像 - REACT
How to Delete Image from Firebase cloud Storage - REACT
我的 Storage
, Post/ID/image.jpg 中有以下结构。 Post 和ID 是很容易找到的文件夹,但ID 可能包含多个图像。我怎么可能删除所有这些?
在我的代码中,我有一个函数应该删除 firestore database
中的 Posts 并从 firebase storage
中删除图像引用
抛出的错误是:
TypeError: Cannot read properties of undefined (reading '_throwIfRoot')
删除函数
const DeletePost = async (e) => {
e.preventDefault();
const querySnapshot = await getDocs(collection(db, `${category}Posts`));
querySnapshot.forEach((docx) => {
if (post.AdTitle === docx.data().AdTitle) {
try {
deleteObject(ref(storage, `${category}Posts`, docx.id).child);
deleteDoc(doc(db, `${category}Posts`, docx.id));
} catch (error) {
console.log("error in delete image, " + error);
}
// router.reload();
}
});
};
编辑
StorageReference 上没有任何 child
属性。尝试删除它,如下所示:
deleteObject(ref(storage, `${category}Posts`, docx.id + ".ext")) // .child);
// Make sure you add the file extension at the end
deleteObject()
和 deleteDoc
return 也是一个承诺,结帐也是如此:
按照这个 documentation 我终于想出了一个可行的解决方案,这是更新后的代码。
try {
const listRef = ref(storage, `${category}Posts/${docx.id}/`);
listAll(listRef)
.then((res) => {
res.items.forEach((itemRef) => {
setValues((prevState) => [...prevState, itemRef]);
});
if (values !== "") {
console.log("attempting");
values?.map((value) =>
deleteObject(
ref(storage, `${category}Posts/${docx.id}/${value.name}`)
).then(() => {
console.log("storage success");
})
);
deleteDoc(doc(db, `${category}Posts`, docx.id)).then(() => {
console.log("doc success");
router.reload();
});
}
})
.catch((error) => {
console.log("error : " + error);
})
我的 Storage
, Post/ID/image.jpg 中有以下结构。 Post 和ID 是很容易找到的文件夹,但ID 可能包含多个图像。我怎么可能删除所有这些?
在我的代码中,我有一个函数应该删除 firestore database
中的 Posts 并从 firebase storage
抛出的错误是:
TypeError: Cannot read properties of undefined (reading '_throwIfRoot')
删除函数
const DeletePost = async (e) => {
e.preventDefault();
const querySnapshot = await getDocs(collection(db, `${category}Posts`));
querySnapshot.forEach((docx) => {
if (post.AdTitle === docx.data().AdTitle) {
try {
deleteObject(ref(storage, `${category}Posts`, docx.id).child);
deleteDoc(doc(db, `${category}Posts`, docx.id));
} catch (error) {
console.log("error in delete image, " + error);
}
// router.reload();
}
});
};
编辑
StorageReference 上没有任何 child
属性。尝试删除它,如下所示:
deleteObject(ref(storage, `${category}Posts`, docx.id + ".ext")) // .child);
// Make sure you add the file extension at the end
deleteObject()
和 deleteDoc
return 也是一个承诺,结帐也是如此:
按照这个 documentation 我终于想出了一个可行的解决方案,这是更新后的代码。
try {
const listRef = ref(storage, `${category}Posts/${docx.id}/`);
listAll(listRef)
.then((res) => {
res.items.forEach((itemRef) => {
setValues((prevState) => [...prevState, itemRef]);
});
if (values !== "") {
console.log("attempting");
values?.map((value) =>
deleteObject(
ref(storage, `${category}Posts/${docx.id}/${value.name}`)
).then(() => {
console.log("storage success");
})
);
deleteDoc(doc(db, `${category}Posts`, docx.id)).then(() => {
console.log("doc success");
router.reload();
});
}
})
.catch((error) => {
console.log("error : " + error);
})