反应未处理的拒绝(TypeError):无法读取未定义的属性“_internalPath”

时间:2021-04-05 09:40:49

标签: reactjs firebase google-cloud-firestore

我一直在尝试让 firebase 与 react 一起工作,但遇到了这个错误

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>
    )
}
<块引用>

未处理的拒绝(类型错误):无法读取未定义的属性“_internalPath” 拿来 D:/Peti/Programming/node/coursewebpage/src/components/CoursesPage.js:12 9 | const db = firebase.firestore() 10 | const myAuthLevel = (firebase.auth().currentUser != null) ?等待 (等待 db.collection('users').doc(firebase.auth().currentUser.uid).get()).data().authLevel : 0 11 |控制台日志(myAuthLevel) 12 | const course = await db.collection("courses").where(myAuthLevel, '>=', 'authLevel').get()// orderBy('createdAt').get() | ^ 13 |控制台日志(课程) 14 | } 15 | //const course = await getFirebase().firestore().collection('courses').get()

1 个答案:

答案 0 :(得分:0)

您收到此错误是因为查询或身份验证出现问题,并且由于您的应用无法解析 fetch 函数中的承诺而出现意外状态,请将此函数的所有代码包装在try/catch 您可能会得到一个不同的错误,它比您所知道的更有帮助。所以这样做:

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);
    }
}