我得到了错误

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

当前回答

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

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

其他回答

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

发展:

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

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

生产:

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

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

经过批准的答案是非常危险的,因为任何人都可以在没有任何许可的情况下读取或写入您的数据库。我建议用这个。

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

这将允许授权人员写入数据库,而任何人都可以读取数据库,以防访问者访问网站。

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

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

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