小编Joh*_*ugh的帖子

Firestore 安全规则获取参考字段/ID

我有两个集合 - 租户和用户。

租赁文档有一个名为“landlordID”的字段,其类型为 REFERENCE(不是字符串)。

现在在我的 Firestore 安全规则中,我希望仅当该租户的 ownerID 字段与发出请求的用户的 uid 匹配时才允许更新租户,即request.auth.uid.

将其读作“如果创建用户的用户经过身份验证,则允许更新租赁文档,因此 request.auth.uid != null,并且 ownerID 字段的 ID 应等于 request.auth.uid 的 ID。

因此代码应该是这样的:

    service cloud.firestore {

      match /databases/{database}/documents {

        match /tenancies/{tenancyID}{

            allow update: if request.auth.uid != null && 
                        request.auth.uid == get(resource.data.landlordID).id
    }

}
Run Code Online (Sandbox Code Playgroud)

我也试过 get(/databases/$(database)/documents/users/$(resource.data.landlordID)).data.id

支持我的数据库截图

在此处输入图片说明

这应该很简单,但 get() 根本不起作用。Firebase Docs,滚动到“访问其他文档”对我的情况根本没有帮助,我不知道如何让它工作。

如果不能像这样使用引用,那将是一种耻辱,因为它们就像文档的任何其他字段一样。

firebase firebase-security google-cloud-firestore

8
推荐指数
2
解决办法
2585
查看次数