firestore规则函数get()和存在()未返回所需结果。我在做什么错?

发布于 2025-02-04 02:09:51 字数 1148 浏览 6 评论 0原文

这是我正在使用的Firestore数据库的准排骨结构。 名称均为小写

编辑

  1. 所有 居民子集合

1的工作目的是 2不是

match /databases/{database}/documents {

  match /hostels/{hostelId} {
    allow read, write: if request.auth.uid != null;

    match /rooms/{roomId} {
     allow read, write: if exists(/databases/$(database)/documents/
                               hostels/$(hostelId)/admins/$(request.auth.uid));

     allow read: if exists(/databases/$(database)/documents/
                         hostels/$(hostelId)/rooms/$(roomId)/residents/$(request.auth.uid));

       }
    }
}

在阅读室作为居民时,我会遇到错误,

Null value error. for 'list' @ L49

我也通过阅读居民文档的布尔领域而尝试使用Get(),没有运气。

我在做什么?是否存在限制()和get()路径参数深度?任何帮助都将是

我用来查询居民的代码,

let q = collection(this.hostelCollection, 
           this.residentSnap.get('hostel_id'), "rooms")
let docs = await getDocs(q);

居民文档具有Hostel_ID字段,其中包含Hostel ID

enter image description here

This is barebone structure of the Firestore database that I am using.
edit: all names are in small letters

The rules I want is

  1. admins of a hostel can read all the rooms of the hostel
  2. residents of a room can only view the room if the room has the residents id in its
    residents subcollection

1 is working as intented
2 is not

match /databases/{database}/documents {

  match /hostels/{hostelId} {
    allow read, write: if request.auth.uid != null;

    match /rooms/{roomId} {
     allow read, write: if exists(/databases/$(database)/documents/
                               hostels/$(hostelId)/admins/$(request.auth.uid));

     allow read: if exists(/databases/$(database)/documents/
                         hostels/$(hostelId)/rooms/$(roomId)/residents/$(request.auth.uid));

       }
    }
}

while reading rooms as resident i get error

Null value error. for 'list' @ L49

I also tried with get() by reading a boolean field of resident document and no luck.

what am i doing worng? Is there a limit to exists() and get() path argument depth? Any help would be much appriciated

the code i use to query rooms by resident is

let q = collection(this.hostelCollection, 
           this.residentSnap.get('hostel_id'), "rooms")
let docs = await getDocs(q);

resident document has hostel_id field containing hostel id

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2025-02-11 02:09:51

尝试添加单个允许阅读语句并重构规则,如下所示:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /hostels/{hostelId} {
      allow read, write: if request.auth.uid != null;

      match /rooms/{roomId} {
        function isAdmin() {
          return exists(/databases/$(database)/documents/hostels/$(hostelId)/admins/$(request.auth.uid));
        }
  
        function isResident() {
          return exists(/databases/$(database)/documents/hostels/$(hostelId)/rooms/$(roomId)/residents/$(request.auth.uid));
        }
      
        allow read: if isAdmin() || isResident();
        allow write: if isAdmin(); 
      }
    }
  }
}

Try adding a single allow read statement and refactoring the rules as shown below:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /hostels/{hostelId} {
      allow read, write: if request.auth.uid != null;

      match /rooms/{roomId} {
        function isAdmin() {
          return exists(/databases/$(database)/documents/hostels/$(hostelId)/admins/$(request.auth.uid));
        }
  
        function isResident() {
          return exists(/databases/$(database)/documents/hostels/$(hostelId)/rooms/$(roomId)/residents/$(request.auth.uid));
        }
      
        allow read: if isAdmin() || isResident();
        allow write: if isAdmin(); 
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文