我得到了错误

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

当前回答

对我来说,是日期的问题。更新后问题已解决。

允许读/写:

 if request.time < timestamp.date(2020, 5, 21);

编辑:如果您仍然感到困惑,无法找出问题所在,请查看firebase控制台的规则部分。

其他回答

有很多很好的答案,但由于这是Firestore许可拒绝错误的顶级谷歌响应,我想我应该为初学者和新手添加一个答案。

为什么要设置安全规则?

如果您编写自己的后端,您将让用户向服务器请求一些东西,服务器将决定允许他们做什么。例如,您的服务器不允许user1删除user2的所有数据。

但是由于你的用户直接与Firebase交互,你不能真正信任你的用户发送给你的任何东西。例如,User1可以将删除请求中的用户id更改为'user2'。

Firestore安全规则是你告诉Firestore即你的后端服务器,谁被允许读取和写入什么数据。

Firestore团队的这个视频非常有用。

https://www.youtube.com/watch?v=eW5MdE3ZcAw

如果你遇到“权限缺失或权限不足”的错误,我强烈建议你在尝试其他任何东西之前观看完整的22分钟视频。

我从1/10到7/10理解了安全规则是如何工作的,以及为什么我只从这个视频中得到了错误。

入门指南也很有用,可以帮助回答视频中没有涵盖的问题。https://firebase.google.com/docs/firestore/security/get-started

NPM I—save firebase @angular/fire

在app.module中确保你导入了

import { AngularFireModule } from '@angular/fire';
import { AngularFirestoreModule } from '@angular/fire/firestore';

进口

AngularFireModule.initializeApp(environment.firebase),
    AngularFirestoreModule,
    AngularFireAuthModule,

在实时数据库规则中,确保你有

{
  /* Visit  rules. */
  "rules": {
    ".read": true,
    ".write": true
  }
}

在云壁炉规则确保你有

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

原始代码:

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

所以在我的情况下,我有以下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会评估一个 根据其潜在结果集而不是实际字段查询 所有文档的值。查询是否可能返回 客户端没有权限读取的文档,全部删除 请求失败。