简体   繁体   English

如何使用 react 从集合中删除/编辑 Firestore 文档

[英]How delete/edit Firestore document from collection with react

So I have a simple CRUD App with React using Firebase Firestore, and I have a bunch of collection.所以我有一个简单的 CRUD 应用程序和 React 使用 Firebase Firestore,我有一堆集合。 Inside each collection I have images object with images.在每个集合中,我都有带有图像的图像对象。 I can't find a way to delete or edit these images.我找不到删除或编辑这些图像的方法。 For the moment I'm able to fetch and displays images by there collection.目前我可以通过那里的集合获取和显示图像。

There is my code.有我的代码。 I want to add delete and an edit button inside the .我想在 .xml 文件中添加删除和编辑按钮。

Please help...请帮忙...


import React, { useState, useEffect } from "react";
import { useRouteMatch, Link } from "react-router-dom";
import { NewPhoto } from "./NewPhoto";
import { app } from "./base";

const db = app.firestore();

export const Album = () => {
  const [images, setImages] = useState([]);
  const [albumName, setAlbumName] = useState("");

  const match = useRouteMatch("/:album");
  const { album } = match.params;

  useEffect(() => {
    const unmount = db.collection("albums")
      .doc(album)
      .onSnapshot((doc) => {
        setImages(doc.data().images || []);
        setAlbumName(doc.data().name);
      });
      return unmount
  }, []);

  return (
    <>
      <section>
        <header>
          <h1>{albumName}</h1>
          <p>Go to the <Link to="/">Home page</Link></p>
        </header>
        {images.map((image) => (
          <aside key={image.name}>
            <img src={image.url} alt="album" />
          </aside>
        ))}
      </section>
      <footer>
        <NewPhoto currentAlbum={album} />
      </footer>
    </>
  );
};

I'm unsure what you are trying to achieve or what your document looks like, is your url image as a string in the doc or is it stored in the firebase storage.我不确定您要实现的目标或文档的外观,您的 url 图像是文档中的字符串还是存储在 firebase 存储中。

Deleting docs is relatively simple as long as you have the doc reference, see these guides on how to delete documents or if you want to delete a field within a doc see these guides .删除文档相对简单,只要您有文档参考, 请参阅有关如何删除文档的指南,或者如果您想删除文档中的字段, 请参阅这些指南 If you want to remove your image from storage look at this doc .如果您想从存储中删除您的图像,请查看此文档

Alternatively if you just want to update that string your can usethese docs to guide you或者,如果您只想更新该字符串,您可以使用这些文档来指导您

To add that functionality to your code under a button you could do something like what is detailed in this post要将该功能添加到按钮下的代码中,您可以执行类似于本文中详述的操作

Maybe something like this would work for you;也许这样的事情对你有用;

import React, { useState, useEffect, useCallback } from "react";
import { useRouteMatch, Link } from "react-router-dom";
import { NewPhoto } from "./NewPhoto";
import { app } from "./base";

const db = app.firestore();

export const Album = () => {
  const [images, setImages] = useState([]);
  const [albumName, setAlbumName] = useState("");
  const [isSending, setSending] = useState(false);

  const match = useRouteMatch("/:album");
  const { album } = match.params;

  const deleteDoc =  useCallback((indexToRemove) => {
    setSending(true);
    db.collection("albums")
      .doc(album)
      .update('images', Firestore.FieldValue.arrayRemove(indexToRemove))
   .then (()=>{
      setSending(false);
      console.log("Album deleted");   
    });
   .catch (error => {
      setSending(false);
      console.log(error);
    });

  },[])

  useEffect(() => {
    const unmount = db.collection("albums")
      .doc(album)
      .onSnapshot((doc) => {
        setImages(doc.data().images || []);
        setAlbumName(doc.data().name);
      });
      return unmount
  }, []);

  return (
    <>
      <section>
        <header>
          <h1>{albumName}</h1>
          <p>Go to the <Link to="/">Home page</Link></p>
        </header>
        {images.map((image, index) => (
          <aside key={image.name}>
            <img src={image.url} alt="album" />
          </aside>
        ))}
      <input type="button" disabled={isSending} onClick={deleteDoc(index)} />
      </section>
      <footer>
        <NewPhoto currentAlbum={album} />
      </footer>
    </>
  );
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM