简体   繁体   English

Firestore 快照侦听器:“null 不是对象”错误

[英]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 应用程序执行的操作:

  1. If the user is signed in, activate snapshot listener and save the value as a state.如果用户已登录,请激活快照侦听器并将值保存为 state。
  2. If the user is signed out, set the state as null.如果用户已注销,请将 state 设置为 null。

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.我在这里看到两个问题。

  1. 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或用户退出时的任何内容)来澄清。
  2. Your snapshot is probably still listening and throwing said error because you haven't unsubscribed from it.您的快照可能仍在侦听并抛出上述错误,因为您尚未取消订阅它。 The React documentation has a good section on cleanup from subscribe functions . React 文档中有一个关于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.

相关问题 取消订阅 Firestore 文档快照侦听器 - Unsubscribe to a firestore document snapshot listener 为快照 Firestore 中的每个元素添加事件侦听器 - Add event listener for each element in snapshot Firestore 添加 Firestore 规则“&& request.auth.uid == userId;” 导致“快照侦听器错误:FirebaseError:缺少或权限不足。” - Adding Firestore rule "&& request.auth.uid == userId;" results in"Error in snapshot listener: FirebaseError: Missing or insufficient permissions." Firebase Firestore - 创建快照事件侦听器是否会花费过多的下载? - Firebase Firestore - does creating a snapshot event listener cost excessive downloads? 自定义 Object 用于 Node.js 中的 Cloud Firestore 在快照中运行。数据不是 function 错误 - Custom Object for Cloud Firestore in Node.js runs in snapshot.data is not a function error Firestore 中的实时侦听器抛出类型错误 - Realtime listener in Firestore throws type error Firestore:使用具有复杂安全规则的快照侦听器时“权限缺失或权限不足” - Firestore: 'Missing or insufficient permissions' when using snapshot listener with complex security rules Memory 多人游戏泄漏/无限循环问题:React State 和 Firestore 快照侦听器 - Memory leak/infinite loop issue with multiplayer game: React State and Firestore snapshot listener 快照侦听器中未捕获的错误:FirebaseError:找不到匹配的索引 - Uncaught Error in snapshot listener: FirebaseError: no matching index found 快照侦听器中未捕获的错误。 超出 Firebase 配额 - Uncaught Error in snapshot listener. Firebase quota exceeded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM