【发布时间】:2022-06-20 15:57:09
【问题描述】:
每当我尝试在我的云 Firestore 集合“文档”中为用户的文档制作挂钩时,我都会收到错误消息:
TypeError:无法读取 null 的属性(读取 'uid')
我认为这是因为当页面运行此代码时,user.uid 尚未加载。但是,我不确定如何让我的应用在创建此钩子之前等待用户加载。
对于我正在使用的这个应用程序:
- Next.js (TypeScript)
- Firebase v9
- React Firebase Hooks v4
const [user, loading, error] = useAuthState(auth);
// https://github.com/CSFrequency/react-firebase-hooks/tree/v4.0.2/auth#useauthstate
const [documents, documentsLoading, documentsError] = useDocument(
doc(db, "documents", user.uid)
);
// https://github.com/CSFrequency/react-firebase-hooks/tree/v4.0.2/firestore#usedocument
【问题讨论】:
-
请提供更多代码。没有人会帮助你,因为他们不知道
useAuthState()和useDocument()的作用相同。
标签: reactjs firebase react-hooks firebase-authentication next.js