Skip to content
Advertisement

How to catch Firebase promise in React?

I have a simple function that checks if the user has Premium access or not:

JavaScript

I tried to catch this in various ways, but no luck, it always returns an “undefined” object.

JavaScript

What is the correct way to get the returned Boolean value?

Advertisement

Answer

onSnapshot is meant for listening to a collection continuously, getting repeatedly notified as its value changes. It does not create a promise, so the promise returned by getPremium is unrelated to the data you will eventually get in onSnapshot. If you just want to get the value once, you should use getDocs:

JavaScript

Also, instead of getting all the users and then using client side code to find the one with the right id, you could just fetch that individual doc directly:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement