我得到了错误

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());
             }
         }
     });

当前回答

在指定安全规则后,我还出现了“缺少或权限不足”错误。事实证明,默认情况下规则不是递归的!例如,如果你写了一个规则

match /users/{userId} {
  allow read, write: if request.auth != null && request.auth.uid == userId;
}

该规则将不适用于/users/{userId}下的任何子集合。这就是我犯错误的原因。

我通过指定规则来修复它:

match /users/{userId}/{document=**} {
  allow read, write: if request.auth != null && request.auth.uid == userId;
}

在文档的相关部分中阅读更多信息。

其他回答

此外,如果您的代码中的集合引用与firebase上的集合名称不匹配,则可能会出现此错误。

例如,firebase上的集合名称是users,但您使用db.collection(" users ")或db.collection("user")引用它。

它也是区分大小写的。

希望这对大家有所帮助

对我来说,问题是AppCheck在我的Firestore控制台也被激活了。 所以我必须按照指南中所述的应用程序检查颤振指南

https://firebase.google.com/docs/app-check/flutter/debug-provider?hl=it&authuser=0

打开androidDebugProvider: true,从控制台复制调试令牌,并将其粘贴到Firestore部分(AppCheck—> app—> add调试令牌),它立即工作。

问题是您试图在用户身份验证之前将数据读或写到实时数据库或firestore。请尝试检查代码的范围。 希望有帮助!

我已经设法解决了我的问题。 我有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数据库。

时间限制可能已过

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    // This rule allows anyone on the internet to view, edit, and delete
    // all data in your Firestore database. It is useful for getting
    // started, but it is configured to expire after 30 days because it
    // leaves your app open to attackers. At that time, all client
    // requests to your Firestore database will be denied.
    //
    // Make sure to write security rules for your app before that time, or else
    // your app will lose access to your Firestore database
    match /{document=**} {
      allow read, write: if request.time < timestamp.date(2020,7, 1);
    }
  }
}

这一行更改了日期:

 allow read, write: if request.time < timestamp.date(2020,7, 1);