简体   繁体   English

使用 React Native Firebase 消息应用程序在 OnSnapshot 上收听消息时出现问题

[英]Problem with listening to messages OnSnapshot with a React Native Firebase Messaging App

I'm trying to create a React Native messaging app with the firebase SDK. In the chat screen I am trying to listen to updated messages in my firestore database.我正在尝试使用 firebase SDK 创建 React Native 消息传递应用程序。在聊天屏幕中,我正在尝试收听我的 firestore 数据库中的更新消息。 I'm following a lot of the code shown in this github repository https://github.com/ReactNativeSchool/react-native-firebase-chat-app , but it uses react-native-firebase and I am using the SDK which is causing making it hard for me to find the equivalent code with the firebase SDK. What am I doing wrong in the below code that is giving me the following error when I open the screen:我正在关注这个 github 存储库https://github.com/ReactNativeSchool/react-native-firebase-chat-app中显示的很多代码,但它使用 react-native-firebase 而我使用的是 SDK导致我很难找到与 firebase SDK 等效的代码。我在下面的代码中做错了什么,当我打开屏幕时出现以下错误:

undefined is not a function (near '...(0,_firebaseConfig.listenToMessages)(threadID).onSnapshot...') undefined 不是 function(靠近 '...(0,_firebaseConfig.listenToMessages)(threadID).onSnapshot...')

I believe it has to do with me not converting from react-native-firebase to the firebase SDK correctly, but I'm not sure.我相信这与我没有从 react-native-firebase 正确转换为 firebase SDK 有关,但我不确定。

Below is my listenToThreads code from the firebaseConfig file where I do all my firebase functions.下面是来自 firebaseConfig 文件的 listenToThreads 代码,我在其中执行所有 firebase 函数。 Below that is the part I commented out that returned the values within that collection.下面是我注释掉的部分,它返回了该集合中的值。

export const listenToMessages = async (threadID) => {
  return firebase.firestore()
    .collection('threads')
    .doc(threadID)
    .collection('messages');

  // try {
  //   const q = query(collection(db, "threads"), where("tid", "==", threadID));
  //   const doc = await getDocs(q);
  //   const data = doc.docs[0].data();
  //   return data.messages;
  // } catch {
  //   return []; 
  // }
};

and here is my onSnapshot code which I'm running inside a working UseFocusEffect hook.这是我在工作的 UseFocusEffect 挂钩中运行的 onSnapshot 代码。

const unsubscribe = listenToMessages(threadID).onSnapshot(
    querySnapshot => {
      const formattedMessages = querySnapshot.docs.map(doc => {
        return {
            _id: doc.id,
            text: '',
            createdAt: new Date().getTime(),
            user: {}
        };
      });

      setMessages(formattedMessages);
    },
);

return () => {
    unsubscribe();
};

The listenToMessages function should not be async . listenToMessages function 不应该是async

It returns a promise rather than the doc you want.返回 promise而不是您想要的文档。 ✌️ ✌️

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 angular 应用收听 firebase 云消息时出现问题 - problem listening to firebase cloud messaging via angular app 在 React Native 的应用程序消息中安装 Firebase 时出错 - Error Installing Firebase In App Messaging in React Native React-native-firebase 应用内消息不工作 - React-native-firebase in-app-messaging not working react-native 应用程序在没有登录的情况下崩溃 Firebase 使用带有“where”查询和“in”运算符的“onSnapshot”时身份验证注销 - react-native app crashes without log on Firebase auth logout when using 'onSnapshot' with 'where' query and 'in' operator 在 Firebase 云消息传递中,React 本机应用程序未通过 TOPIC 接收通知 - React native app not receiving notifications via TOPIC in Firebase cloud messaging 将 Firebase 链接到 React Native 应用程序后出现问题 - Problem after linking Firebase to a React Native app Firebase 和 React Native 订阅更改 (onSnapshot) 无法正常工作 - Firebase and React Native subcribe to changes (onSnapshot) not working properly react-native-firebase_messaging:compileReleaseJavaWithJavac - react-native-firebase_messaging:compileReleaseJavaWithJavac 应用消息中的 Firebase 在我的 React Native 项目中不起作用。 有人有同样的问题吗? - Firebase in app messaging is not working in my react native project. Anyone having the same issue? 打开应用程序后如何删除 react-native-firebase/messaging 中的通知标记计数 - How to remove the notification badge count in react-native-firebase/messaging after opening the app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM