Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

341
Views
Firestore rules functions get() and exists() not returning desired result. What am I doing wrong?

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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(); 
      }
    }
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!