Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am having an issue when trying to retrieve data from Firestore using the Firebase JS SDK

I am having an issue when trying to retrieve data from Firestore using the Firebase JS SDK. I am receiving the following error:

TypeError: firebase_firestore__WEBPACK_IMPORTED_MODULE_3__.getDoc(…).data is not a function

I am trying to load the data using the following code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

useEffect(() => {
    const getAuthorData = async () => {
        setAuthorData(
            await getDoc(doc(db, 'users', post.data.author)).data()
        )
    }
    const p = getAuthorData()
    console.log(p)
}, [])

I think have imported the necessary Firebase modules and initialized the app with the correct configuration. I have also checked that the getDoc function is returning a Firestore DocumentSnapshot object, but the data() method is not a function. Some data is showing behin the error

I would like to know if there is a problem with my code or if I am missing something else.

Any help would be appreciated, thanks!

>Solution :

The problem is that getDoc returns an object that does not have a method called data. It returns a promise. Your code right now is trying to call data() on that promise instead of first awaiting it. If you are trying to keep this at one line of code, you will have to force the await to happen first before the call to data() by using parenthesis:

(await getDoc(doc(db, 'users', post.data.author))).data()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading