如何解决Firestore:为什么在操场上运行而不是在应用程序中运行作为查询
数据模型:我们有一个集合 users
。每个用户都有一个子集合 posts
,每个帖子都有一个子集合 comments
。关键是每个帖子都有一个字段 viewers
,它是一个可以查看帖子的用户 ID 数组。所以评论的路径是:
users/{userid}/posts/{postid}/comments/{commentid}
规则是:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow write: if request.auth != null && request.auth.uid == userId;
allow read : if request.auth != null
}
match /users/{userId}/posts/{postId} {
allow read,write: if request.auth != null && ((request.auth.uid == userId) || (request.auth.uid in resource.data.viewers))
allow update: if request.auth != null && ((request.auth.uid == userId) || (request.auth.uid in resource.data.viewers))
}
match /users/{userId}/posts/{postId}/comments/{commentId} {
allow create,write : if request.auth != null && request.auth.uid in get(/users/userId/posts/postId).data.viewers
allow read: if request.auth != null && (request.auth.uid == userId || request.auth.uid==resource.data.uid)
}
}
}
在规则(操场)中,当我检查以获取像 /users/userId1/posts/postId 这样的特定路径时,并且在这篇博文中,数组查看器的 ID 为 userId2,工作正常(正确)。但是在应用程序中,userId2 无法查看其他帖子并显示错误:
失败:状态{代码=PERMISSION_DENIED,描述=权限缺失或不足。,原因=空}
应用内查询:
db.collectionGroup("posts")
.whereArrayContains("viewers",user.getUid())
.whereEqualTo("visible","visible").get()
知道为什么会发生这种情况吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。