Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

337
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda