[英]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.