gpt4 book ai didi

reactjs - react 未处理的拒绝(TypeError): Cannot read property '_internalPath' of undefined

转载 作者:行者123 更新时间:2023-12-05 04:51:24 31 4
gpt4 key购买 nike

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

Unhandled Rejection (TypeError): Cannot read property '_internalPath' of undefinedfetchD:/Peti/Programming/node/coursewebpage/src/components/CoursesPage.js:129 | const db = firebase.firestore()10 | const myAuthLevel = (firebase.auth().currentUser != null) ? await (await db.collection('users').doc(firebase.auth().currentUser.uid).get()).data().authLevel : 011 | 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()

最佳答案

您收到此错误是因为查询或身份验证出现问题,并且由于您的应用无法解析 fetch 函数中的 promise 而出现意外状态,包装您的所有代码围绕 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);
}
}

关于reactjs - react 未处理的拒绝(TypeError): Cannot read property '_internalPath' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66950980/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com