gpt4 book ai didi

java - 如何从 Firestore 中删除选定的文档 ID

转载 作者:行者123 更新时间:2023-12-02 02:15:51 24 4
gpt4 key购买 nike

如标题所示。我有一个字符串 shoppingListId ,它保存当前在 RecyclerView 文档 ID 中单击的内容,我想知道如何删除此选定的文档 ID。

我尝试了以下方法,但由于类型不兼容而不起作用:

FirebaseFirestore docRef = FirebaseFirestore.getInstance();
DocumentReference selectedDoc = docRef.collection("products").document(shoppingListId);
selectedDoc.delete();

如何获取DocumentReference的Instance,然后删除选中的文档?我想这可能很简单,但我坚持了下来。

View of database

更改了代码,目前没有任何错误,但仍然不起作用。

最佳答案

我认为您正在关注此 tutorial ,这实际上是我做的。

您的代码中的问题在于以下行:

DocumentReference selectedDoc = docRef.collection("products").document(shoppingListId);

您无法仅使用该行代码来删除产品。您的代码不完整。要解决此问题,请将该行更改为:

DocumentReference productIdRef = rootRef.collection("products").document(shoppingListId)
.collection("shoppingListProducts").document(productId);
productIdRef.delete().addOnSuccessListener(aVoid -> Snackbar.make(shoppingListViewFragment, "Product deleted!", Snackbar.LENGTH_LONG).show());

如果要删除购物列表,首先需要删除其下方的所有内容。因此,为了删除特定的购物 list ,首先您需要找到 shoppingListProducts 集合下的所有文档,将其删除,然后您就可以删除 shoppingListId文档。

关于java - 如何从 Firestore 中删除选定的文档 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49307641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com