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 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
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();
}
}
}
}