[英]Firestore snapshot listener: 'null is not an object' error
Here is what I am trying to do with my React Native app using Firestore:这是我尝试使用 Firestore 对我的 React Native 应用程序执行的操作:
When the user is signed in, the code works just fine.当用户登录时,代码工作得很好。 However, when the user signs out, the error prompt 'null is not an object (evaluating doc.exists)' shows up.
但是,当用户退出时,会出现错误提示“null is not an object (evaluating doc.exists)”。
What might be wrong with the below code?下面的代码可能有什么问题? Any advice?
有什么建议吗? Thanks!
谢谢!
useEffect(()=>{
if (user) {
firestore().collection('points').doc(user.uid).onSnapshot(
(doc)=>{
if (doc.exists){
setPoints(doc.data());
}
else {
setPoints(null);
}
}
)
}
else {
setPoints(null);
}
},[user]);
I see 2 problems here.我在这里看到两个问题。
if (user) {}
is obviously evaluating to true
. if (user) {}
显然评估为true
。 I'd clarify by changing it to if (user !== null
(or undefined
or whatever it is when a user signs out).if (user !== null
(或undefined
或用户退出时的任何内容)来澄清。cleanup
from subscribe functions . cleanup
订阅函数的好部分。 Basically you need to wrap your onSnapshot()
inside a named function, then return that function from the useEffect()
.基本上,您需要将
onSnapshot()
包装在名为 function 的内部,然后从useEffect()
返回该 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.