【问题标题】:React Unhandled Rejection (TypeError): Cannot read property '_internalPath' of undefined反应未处理的拒绝(TypeError):无法读取未定义的属性“_internalPath”
【发布时间】:2021-07-01 04:40:06
【问题描述】:

我一直在尝试让 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>
    )
}

未处理的拒绝 (TypeError):无法读取未定义的属性“_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 | console.log(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()

【问题讨论】:

    标签: reactjs firebase google-cloud-firestore


    【解决方案1】:

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

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

    【讨论】:

    • 我已经尝试过您的解决方案,但错误仍然存​​在 (Cannot read '_internalPath' of undefined)。
    • 尝试将fetch函数移出你的使用效果,并在调用它时通过await fetch()这样做
    猜你喜欢
    • 1970-01-01
    • 2021-12-13
    • 2020-11-27
    • 2017-12-26
    • 2021-05-06
    • 2019-04-07
    • 2019-03-25
    • 2021-03-04
    • 2021-09-16
    相关资源
    最近更新 更多