简体   繁体   English

从 firebase firestore 中删除图像文档

[英]Delete image Doc from firebase firestore

I am trying to delete a document from firebase firestore collection how can I do that, I tried few things but no success here is my code我正在尝试从 firebase firestore 集合中删除一个文档我该怎么做,我尝试了一些但没有成功这是我的代码

here is the Firestore hook这是 Firestore 挂钩


const useFirestore = (somecollection) => {

    const [docs, setDocs] = useState([]);


    useEffect (() => {
        // new collection reference
          const newcoll = collection(projectFirestore, somecollection);
          const q = query(newcoll, orderBy('createdAt', 'desc'));
          const unsub = onSnapshot(q, (snapshot) => {
            let documents = [];
            snapshot.forEach(doc => {
               documents.push({...doc.data(), id: doc.id})
            })
            setDocs(documents);
          });
     
          return () => unsub;

    },[somecollection]);
    
    return { docs };
}

the imagegrid where all the images are shown (only the js without the jsx return)显示所有图像的图像网格(只有 js 没有 jsx 返回)

const { docs } = useFirestore('images');

  const handleDelete = (e) => {
    docs.forEach(doc => {
      deleteDoc(doc);
    })

now, on the imagegrid jsx I have all the uploaded images that store the document in them.现在,在 imagegrid jsx 上,我拥有所有上传的图像,这些图像将文档存储在其中。

so I added a button to every image and I want that when I click the button its fires a handleDelete() that delete this specific image document from the firestore所以我为每个图像添加了一个按钮,我希望当我单击该按钮时它会触发一个 handleDelete() 从 firestore 中删除这个特定的图像文档

If you are trying to have a button that deletes a single document, you likely do NOT want to be looping over all of the docs and calling deleteDoc() .如果您尝试使用一个按钮来删除单个文档,您可能不想遍历所有文档并调用deleteDoc()

Instead, your UI would likely display each image and include its ID for each button.相反,您的 UI 可能会显示每个图像并包含每个按钮的 ID。 Something like:就像是:

const deleteOneImageDoc = async (docId) => {
  try {
    let docRef = doc(myFirestore, `images/${docId}`);
    await deleteDoc(docRef);
  } catch (ex) {
    console.error(`Delete FAILED: ${ex.message}`);
    throw ex;
  }
};

return <div>
  docs.map((imgDoc) => {
    return (
      <div key={imgDoc.id}>
        <img src={imgDoc.url}/>
        <button onClick={() => deleteOneImageDoc(imgDoc.id)}>
          delete {imgDoc.id}
        </button>
      </div>);
    })
  </div>;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 flutter onClick 从 firebase 存储和 firestore 中删除图像 - flutter onClick delete image from firebase storage and firestore 使用 React 将图像上传到 Firebase/Firestore v9 中的现有文档 - Upload Image to Existing doc in Firebase/Firestore v9 with React React中如何获取Firebase 9中的多个Doc对象? 并使用其文档 ID 从 firestore 获取多个文档? - How to get multiple Doc objects in Firebase 9 in React? and get multiple docs from firestore using its doc id? 无法显示来自 firebase firestore 和反应 js 的图像 - Can not display Image from firebase firestore and react js 使用 Redux 和 React 时无法从 Firebase 集合中删除数据(doc) - Can't delete data (doc) from Firebase collection when using Redux and React 为什么我收到“类型错误:文档不是函数”(Firebase 9、Firestore) - Why I receive "TypeError: doc is not a function" (Firebase 9, Firestore) Flutter 删除 Firebase Firestore 中数组中的字段 - Flutter Delete Field in Array in Firebase Firestore 无法删除 Firebase Cloud Firestore 中的多个文档 - Cannot delete multiple documents in Firebase Cloud Firestore `firebase_tools.firestore.delete` 需要一个令牌,但该令牌从何而来? - `firebase_tools.firestore .delete` requires a token, but where does that token come from? 从 Firebase Firestore 中删除字段/键带有句点/标点符号 (".") 的字段 - 模块化 v9 JavaScript SDK - Delete a field from Firebase Firestore where the field/key has a period/punctuation (".") - modular v9 JavaScript SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM