In my local environement using the firebase emulator, I have the following rule that raises a FirebaseError: Property managers is undefined on object. for 'list':
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /businesses/{business} {
allow read: if request.auth != null && request.auth.uid in resource.data.managers;
}
}
}
If I test the same rule on the rules Playground, it works correctly (Simulated read allowed)..
To prove I have the managers property defined and available, if I change the rule to if true, I can loop on the list with the following local data:
// code
...
.then((snap) => snap.forEach((doc) => console.log(doc.id, " => ", doc.data())))
// output
VzaU8rX8HOgejk0fyOFK => {..., managers: Array(1)}
What am I doing wrong?
Thank you !
UPDATE
This is the code I would like to have the rule applied on:
useEffect(() => {
db.collection("businesses")
.where("slug", "==", params.slug)
.get()
.then((snap) => {
if (!snap.empty) {
// slug is supposed to be unique, so I retrieve the first and only element
const doc = snap.docs[0]
setBusiness({ id: doc.id, data: doc.data() })
setState({ ...state, loading: false, data: true })
} else {
setState({ ...state, loading: false, error: "Business not found" })
}
})
.catch((error) => {
console.log("Error getting document:", error)
setState({ ...state, loading: false, error: "Error getting document" })
})
}, [])