我得到了错误

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

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

其他回答

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

以上投票的答案对数据库的健康是危险的。你仍然可以让你的数据库只用于读取而不用于写入:

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

检查是否在IAM & Admin https://console.cloud.google.com/iam-admin/iam中添加了服务帐户,并设置了适当的角色,如Editor

进入数据库-> 规则- - - >

发展:

更改允许读,写:如果为false;真正的;

注意:它只是用于开发目的的快速解决方案,因为它将关闭所有安全性。因此,不建议在生产中使用。

生产:

如果从firebase验证:更改允许读,写:如果为false;请求。Auth != null;

https://console.firebase.google.com

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