【问题标题】:Delete item from a subcollection in firebase with react使用反应从firebase中的子集合中删除项目
【发布时间】:2022-07-05 02:28:22
【问题描述】:

我从集合'users'中创建了子集合'medicine',我试图在单击按钮时从该子集合中删除每个项目,这就是我所做的:

import {auth, db} from "../../firebase/firebase.config";
import {doc, deleteDoc} from "firebase/firestore";

function BotonEliminar(id) {

    const user = auth?.currentUser?.uid;

    const deleteElement = async () =>{
       await deleteDoc(doc(db, 'users', user, 'medicine', id));
        
    }

.... etc

我进入了控制台:

Uncaught (in promise) TypeError: n.indexOf is not a function

我不知道我做错了什么。我得到了正确的 id 和用户。


console.log(db, user, id);

数据库的样子:

【问题讨论】:

  • 你能在那个函数中尝试console.log(db, user, id)并分享输出吗?
  • 是的,当然!我在顶部添加捕获

标签: javascript reactjs firebase google-cloud-firestore


【解决方案1】:

问题似乎是id 是一个对象,而不是一个字符串。 (从控制台截图来看,它看起来像{id: 'liKQ1I0RjliCQVKFHvc'} 而不仅仅是'liKQ1I0RjliCQVKFHvc'

假设 BotonEliminar 是一个 React 组件,参数是一个 React props 对象而不是实际的 id。您可能希望将函数签名更改为 destructure 来自道具的 id:

function BotonEliminar({id}) { // note the { and }

然后正常使用id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-08
    • 2011-04-13
    • 2020-08-01
    • 1970-01-01
    • 2012-09-05
    • 2019-11-05
    • 2013-01-17
    • 2012-12-18
    相关资源
    最近更新 更多