Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

340
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda