快照侦听器中的错误:FirebaseError:丢失或不足的权限
我在Google Firebase上有一个Firestore数据库,其中包含“乡村”收藏。 我想将每个文档的读/写功能限制为特定用户的UUID作为文档密钥。
我已经在Firestore的“规则”选项卡中添加了规则,但是当我尝试获取数据时,我会发现一个错误,说我没有对Firestore的许可...
这是我的规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /village/{villageId} {
allow read, write: if request.auth != null && request.auth.uid == villageId;
}
}
}
这是代码段,如果我从firestore中删除我的规则,则成功返回数据:
useEffect(() => {
const collectionRef = collection(db, "village");
const q = query(collectionRef, orderBy("timestamp", "desc"));
const unsubscribe = onSnapshot(q, (querySnapshot: any) => {
setVillage(
querySnapshot.docs.map((doc) => ({
...doc.data(),
id: doc.id,
timestamp: doc.data().timestamp?.toDate().getTime(),
}))
);
});
return unsubscribe;
}, []);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您当前的查询要求所有村庄,并通过时间戳订购。因为您的用户只能访问自己的村庄,所以您需要更改查询以仅针对他们有权访问的东西(请记住,Firestore安全规则不是过滤)。
要更新您的代码以符合所定义的规则,您需要从所有村庄的查询切换到他们的村庄。
Your current query is requesting all villages, and ordering them by their timestamp. Because your user can only access their own village, you need to change your query to only target what they have access to (remember that Firestore Security Rules are not filters).
To update your code to work with the rules you defined, you need to switch from a query of all villages, to just their village.
可以帮助某人,就我而言,发生同样的错误,因为在Firestore规则中,我有
request.time< timestamp.date(2022,8,6);
将日期更改为以后的时间点解决了我的问题。May it help someone, in my case the same error occurred because in Firestore rules I had
request.time < timestamp.date(2022, 8, 6);
Changing the date to a later point in time solved my issue.