【问题标题】:null is not an object (evaluating 'snapshot.forEach')null 不是对象(评估 'snapshot.forEach')
【发布时间】:2021-04-30 17:03:41
【问题描述】:

我正在尝试订阅 Firestore 上的查询,但添加过滤器时出现错误。

这很好用

  useEffect(() => {
    if (dbChats && currentUser?.uid) {
      const unsubscribe = dbChats
        .orderBy('createdAt')
        .limit(100)
        .onSnapshot((querySnapshot) => {
          const chats = firebaseLooper(querySnapshot);

          setChats(chats);
        });
      return unsubscribe;
    }
  }, [dbChats]);

但这不是

  useEffect(() => {
    if (dbChats && currentUser?.uid) {
      const unsubscribe = dbChats
        .where('participants', 'array-contains', currentUser.uid)
        .orderBy('createdAt')
        .limit(100)
        .onSnapshot((querySnapshot) => {
          const chats = firebaseLooper(querySnapshot);

          setChats(chats);
        });
      return unsubscribe;
    }
  }, [dbChats]);

每当我添加 where('participants', 'array-contains', currentUser.uid)

它会抛出错误

null is not an object (evaluating 'snapshot.forEach')

请注意,这也有效

dbChats.where('participants', 'array-contains', currentUser.uid).get()

【问题讨论】:

  • Afaik 快照不应该为空。我唯一能想到的是它可能会返回一个错误,但是您没有为 onSnapshot 提供任何错误处理(它是第二个参数)。或者firebaseLooper 中发生了一些有趣的事情,但你没有向我们展示。
  • 感谢 onError 参数的见解。我错过了一个索引The query requires an index. 如果你想对此做出回答,我会接受它为有效的
  • 酷酷。顺便说一句,每个新查询都需要在 firestore 中创建一个新索引。没有索引它什么也做不了。
  • 很好,我对 Firestore 还很陌生,不知道这一点非常感谢

标签: firebase react-native react-native-firebase


【解决方案1】:

snapshot 为空,因为您没有提供任何错误处理并且查询抛出错误。只需将错误处理程序作为第二个参数提供给 onSnapshot

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 2021-10-20
    • 2016-06-08
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多