我得到了错误

gettingdocuments.com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED:缺少或权限不足。

对于下面关于else语句的代码

db.collection("users")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
             if (task.isSuccessful()) {
                 for (DocumentSnapshot document : task.getResult()) {
                     s(document.getId() + " => " + document.getData());
                 }
             } else {
                 s("Error getting documents."+ task.getException());
             }
         }
     });

当前回答

我已经设法解决了我的问题。 我有2个错误:

PERMISSION_DENIED:缺少或权限不足。 不能为此项目启用Firestore。

修复此问题的步骤:

第1部分。

访问https://console.firebase.google.com/ Firestore Database ->删除所有集合 Firestore Database -> Rules ->删除所有规则历史记录

第2部分。

访问https://console.cloud.google.com/ 在菜单中找到:api和服务->已启用的api和服务 禁用3个服务:“云Firestore API”,“Firebase规则API”,“Firebase API的云存储” Firestore ->将自动启用“云Firestore API”和“Firebase规则API”服务,并将创建Firestore数据库。 启用“Firebase API的云存储”。

最重要的是从谷歌云控制台创建一个Firestore数据库。

其他回答

确保你的数据库不是空的,你的查询是不存在的集合

所以在我的情况下,我有以下DB规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /stories/{story} {
      function isSignedIn() {
        return request.auth.uid != null;
      }
    
      allow read, write: if isSignedIn() && request.auth.uid == resource.data.uid
    }
  }
}

正如您所看到的,故事文档上有一个uid字段用来标记所有者。

然后在我的代码中,我查询了所有的故事(Flutter):

Firestore.instance
          .collection('stories')
          .snapshots()

它失败了,因为我已经通过不同的用户添加了一些故事。 要解决这个问题,你需要在查询中添加条件:

Firestore.instance
          .collection('stories')
          .where('uid', isEqualTo: user.uid)
          .snapshots()

更多详情请访问:https://firebase.google.com/docs/firestore/security/rules-query

编辑:从链接

规则不是过滤器 在编写检索文档的查询时,请保持 请记住,安全规则不是过滤器—查询都是或 什么都没有。为了节省您的时间和资源,Cloud Firestore会评估一个 根据其潜在结果集而不是实际字段查询 所有文档的值。查询是否可能返回 客户端没有权限读取的文档,全部删除 请求失败。

原始代码:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
          
    }
  }
}

修改代码:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
       allow read: if true;
       allow write: if request.auth != null;
    }
  }
}

也许你该把日期去掉

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if
          request.time < timestamp.date(2021, 12, 12);
    }
  }
}

https://console.firebase.google.com

开发->数据库->规则->设置读、写-> true