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