Firestore security rules error: [code=permission-denied]: Null value error. for 'list'
![Firestore security rules error: [code=permission-denied]: Null value error. for 'list'](/_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2F80wy3gkl%2Fproduction%2F54e30bbcb677f725db7afda2ea2f51db97c37e46-1201x631.png%3Fh%3D1000&w=3840&q=75)
I'm writing my security rules for my Firebase chat app, but I'm getting the following error:
Uncaught Error in snapshot listener: FirebaseError: [code=permission-denied]: Null value error. for 'list' @ L77
Line 77 points to this function, where I check if the chat document exists, and that the person requesting access to the document is a participant in the chat.
function isChatParticipant(chatId) {
let chat = get(/databases/$(database)/documents/chats/$(chatId));
return chat.exists() && chat.data != null && chat.data is map &&
(
(chat.data.keys().has('customerUid') && isUser(chat.data.customerUid)) ||
(chat.data.keys().has('sellerUid') && isUser(chat.data.sellerUid))
);
}
I assume this error is occuring because no chat documents exist at the moment, but I'm not sure why my check chat.exists()
does not prevent the error.
How can I avoid this error when the document I'm referencing does not exist?
Answer
The get() function returns a Resource object (or null), and as you can seen from the linked API documentation, a Resource doesn't have an "exists" method on it. If you want to check if a document exists, can simply check the return value for null. Or, you can call the dedicated exists() function first.
Also, there is no point in checking the data property for null or testing if it's a map, since the API documentation says that it's always going to be a non-null Map (and errors cause a rule to reject access anyway).
Enjoyed this question?
Check out more content on our blog or follow us on social media.
Browse more questions