4

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()

2 Answers 2

2

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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried your solution, but the error remains the same (Cannot read '_internalPath' of undefined).
Try moving the fetch function out of your use effect and when calling it do so by await fetch()
1

update where() function:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.