首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Firestore中删除收藏中的所有文档

如何在Firestore中删除收藏中的所有文档
EN

Stack Overflow用户
提问于 2018-11-01 02:02:42
回答 7查看 22.3K关注 0票数 20

我有一个火库数据库。我的项目插件:

_

firestore:^0.7.4 firebase

_

存储:^1.0.1

这有一个具有多个文档的集合"messages“。我需要删除消息集合中的所有文档。但是这段代码失败了:

代码语言:javascript
复制
Firestore.instance.collection('messages').delete();

但没有定义delete

正确的语法是什么?

EN

Stack Overflow用户

发布于 2020-04-05 15:29:20

我认为这可能有助于在任何文档引用中使用多个集合

代码语言:javascript
复制
// add ${documentReference} that contains / may contains the ${collectionsList}
_recursiveDeleteDocumentNestedCollections(
      DocumentReference documentReference, List collectionsList) async {
    // check if collection list length > 0
    if (collectionsList.length > 0) {
      // this line provide an async forEach and wait until it finished
      Future.forEach(collectionsList, (collectionName) async {
        // get the collection reference inside the provided document
        var nfCollectionRef = documentReference.collection(collectionName);
        try {
          // get nested collection documents
          var nestedDocuemtnsQuery = await nfCollectionRef.getDocuments();
          // loop through collection documents 
          Future.forEach(nestedDocuemtnsQuery.documents, (DocumentSnapshot doc) async {
            // recursive this function till the last nested collections documents
            _recursiveDeleteDocumentNestedCollections(
                doc.reference, collectionsList);
            // delete the main document
            doc.reference.delete();
          });
        } catch (e) {
          print('====================================');
          print(e);
          print('====================================');
        }
      });
    }
  }
票数 0
EN
查看全部 7 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53089517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档