Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

What's the best way to handle a doc read with missing permissions? #492

Unanswered
mrtnkb asked this question in Q&A
Discussion options

Hi folks,

I have a document ID which is parsed from a URL query. That means that it could be invalid or point to a document that a user doesn't have security rule permissions to read. My solution for this right now is to catch errors when loading the document:

getDoc(doc(db, "Collection", documentId)).catch((error) => handle(error));

I'd like to switch to using useFirestoreDocData because of the very simple API for realtime updates and the efficient caching of listeners. I was hoping that the returned status field would be useful for indentifying cases where the document isn't available (e.g. invalid ID or missing permissions). This is what I wrote:

const { status, data } = useFirestoreDocData(doc(db, 'Collection', documentId));
if (status === 'loading') {
 // handle loading
} else if (status === 'error') {
 // handle errors
} else {
 // display the document
}

This doesn't work and instead I get an uncaught missing permissions error (including for an invalid document ID). So I have two questions I think:

  1. What is status === 'error' supposed to be used for? I couldn't see anything in the docs or other discussions/issues.
  2. What is the best way to deal with document reads which may fail? I think I could create an error boundary to catch the error but for my use case that's more boilerplate than just not using reactfire.

Thanks a lot : )

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /