React Unhandled Rejection (TypeError): Cannot read property '_internalPath' of undefined

Viewed 1619

I've been trying to get firebase working with react, and got this error

import React, { useState, useEffect } from 'react'
import firebase from 'firebase/app'
import 'firebase/firestore'

export default function CoursesPage(){
    const [courses, setCourses] = useState(<p>Loading courses...</p>)
    useEffect(() => {
        async function fetch(){
            const db = firebase.firestore()
            const myAuthLevel = (firebase.auth().currentUser != null) ? await (await db.collection('users').doc(firebase.auth().currentUser.uid).get()).data().authLevel : 0
            console.log(myAuthLevel)
            const courses = await db.collection("courses").where(myAuthLevel, '>=', 'authLevel').get()// orderBy('createdAt').get()
            console.log(courses)
        }
        fetch()
    },[])

    return(
        <page>
            <h1>Courses</h1>
            {courses}
        </page>
    )
}

Unhandled Rejection (TypeError): Cannot read property '_internalPath' of undefined fetch D:/Peti/Programming/node/coursewebpage/src/components/CoursesPage.js:12 9 | const db = firebase.firestore() 10 | const myAuthLevel = (firebase.auth().currentUser != null) ? await (await db.collection('users').doc(firebase.auth().currentUser.uid).get()).data().authLevel : 0 11 | console.log(myAuthLevel) 12 | const courses = await db.collection("courses").where(myAuthLevel, '>=', 'authLevel').get()// orderBy('createdAt').get() | ^ 13 | console.log(courses) 14 | } 15 | //const courses = await getFirebase().firestore().collection('courses').get()

2 Answers

You are getting this error because something is going wrong either in query or auth and an unexpected state occurs since your app can't resolve the promise in your fetch function, wrap all your code of this function around a try/catch you will likely get a different error which will be more helpful than the one you are getting know. So do this:

async function fetch(){
    try {
        const db = firebase.firestore()
        const myAuthLevel = (firebase.auth().currentUser != null) ? await (await db.collection('users').doc(firebase.auth().currentUser.uid).get()).data().authLevel : 0
        console.log(myAuthLevel)
        const courses = await db.collection("courses").where(myAuthLevel, '>=', 'authLevel').get()// orderBy('createdAt').get()
        console.log(courses)
    } catch (err) {
        console.log(err);
    }
}

update where() function:

where('authLevel','<=',myAuthLevel)
Related