Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:/User/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()

like image 782
Peter Ferencz Avatar asked Oct 16 '25 23:10

Peter Ferencz


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);
    }
}
like image 173
Ralemos Avatar answered Oct 19 '25 13:10

Ralemos


update where() function:

where('authLevel','<=',myAuthLevel)
like image 29
Vikas Rathod Avatar answered Oct 19 '25 11:10

Vikas Rathod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!