问题描述
我正在尝试为Firestore DB编写安全规则,但由于某种原因遇到麻烦。以下代码中的语法对任何人都看起来不正确吗?
match /posts/{postType} {
allow read: if true;
allow write: if get(/databases/$(database)/documents/posts/$(postType)).data.creator_id ==
request.auth.uid;
}
我附上了路径和数据结构的屏幕截图。我遇到的问题是“ get”返回null。
错误:
解决方法
您必须更改规则。
代替:
allow write: if get(/databases/$(database)/documents/posts/$(postType)).data.creator_id == request.auth.uid;
您必须写:
allow write: resource.data.creator_id == request.auth.uid;
,
request.resource.data.creator_id == request.auth.uid
是解决方案!
您没有正确使用模拟器。当要求输入要获取的文档的路径时,必须提供一个实际文档的路径。不要从匹配项中键入通配符。规则将在评估规则时确定通配符的实际值。如果输入“ / posts / {postType}”,则是在告诉它按原样加载ID为“ {postType}”的文档。