firebase v.9 deletedoc()不做任何事情或遇到任何错误

发布于 2025-01-24 02:05:50 字数 1561 浏览 0 评论 0原文

我是React和Firebase的新手,并且我遇到了一个问题,即删除文档功能未被调用并且没有捕获错误。函数deleteitemhandler()确实运行,但是deletedoc()却没有。我已经更改了规则,并确保导入是正确的,但它仍然行不通。

deleteitemhandler(),app.jsx

  //? Delete item function
  const deleteItemHandler = async (id) => {
    try {
      await deleteDoc(doc(db, "items", id));
    }
    catch (error) {
      console.log(error);
    }
  }

删除按钮,yourcard.jsx

        <button
          className="btn btn-danger ms-2"
          onClick={() => {
            // console.log(props.name)
            props.deleteItemHandler(props.id)
          }}
        >
          Delete
        </button>

注意更新: 道具通过 app.jsx to userIdentity.jsx

<UserIdentity
        className="userId"
        currentUser={username}
        loginHandler={loginHandler}
        logoutHandler={logoutHandler}
        addListingHandler={addListingHandler}
        //? for YourCard
        itemsFromDB={itemsFromDB}
        deleteItemHandler={deleteItemHandler}
      />

userIdentity.jsx to yourcard.jsx

{yourItems.map((item) => {
                        return (
                          <YourCard
                            // className="YourCard"
                            name={item.name}
                            id={item.id}
                            key={item.id}
                            deleteItemHandler={props.deleteItemHandler}
                          />
                        );
                      })}

I'm new to React and Firebase, and I got a problem where the delete document function doesn't get called and no errors were caught. The function deleteItemHandler( ) does run, but the deleteDoc( ) doesn't. I have changed rules and make sure the imports are correct, yet it still won't work.

deleteItemHandler( ), App.jsx

  //? Delete item function
  const deleteItemHandler = async (id) => {
    try {
      await deleteDoc(doc(db, "items", id));
    }
    catch (error) {
      console.log(error);
    }
  }

Delete Button, YourCard.jsx

        <button
          className="btn btn-danger ms-2"
          onClick={() => {
            // console.log(props.name)
            props.deleteItemHandler(props.id)
          }}
        >
          Delete
        </button>

Note update:
props passing
App.jsx to UserIdentity.jsx

<UserIdentity
        className="userId"
        currentUser={username}
        loginHandler={loginHandler}
        logoutHandler={logoutHandler}
        addListingHandler={addListingHandler}
        //? for YourCard
        itemsFromDB={itemsFromDB}
        deleteItemHandler={deleteItemHandler}
      />

UserIdentity.jsx to YourCard.jsx

{yourItems.map((item) => {
                        return (
                          <YourCard
                            // className="YourCard"
                            name={item.name}
                            id={item.id}
                            key={item.id}
                            deleteItemHandler={props.deleteItemHandler}
                          />
                        );
                      })}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ繥欢鉨o。 2025-01-31 02:05:50

最终发现问题在于adddoc(),它添加了yc2键入deletedoc() 使用搜索文档的新文档。 MC2类型。将添加方法更改为setDoc()将其修复。

Finally figured out that the problem lies on addDoc(), which adds new document with yc2 type while deleteDoc() searches documents with mc2 type. Changed the add method to setDoc() fixes it.

靑春怀旧 2025-01-31 02:05:50

我以为您没有正确处理

 const App = () => {
    const deleteItemHandler = async (id) => {
        await deleteDoc(doc(db, "cities", id));
    }

 return (
     <YourCard deleteItemHandler={deleteItemHandler} id={someId}}
 ) 

道具

  export const YourCard = ({deleteItemHandler, id}) => {
  
  return (
    <button
      className="btn btn-danger ms-2"
      onClick={() => deleteItemHandler(id)}
    >
      Delete
    </button>
  )

im thinking you're not handling props correctly

 const App = () => {
    const deleteItemHandler = async (id) => {
        await deleteDoc(doc(db, "cities", id));
    }

 return (
     <YourCard deleteItemHandler={deleteItemHandler} id={someId}}
 ) 

child

  export const YourCard = ({deleteItemHandler, id}) => {
  
  return (
    <button
      className="btn btn-danger ms-2"
      onClick={() => deleteItemHandler(id)}
    >
      Delete
    </button>
  )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文