尽管在Firestore数据库中有效率,在下面的示例中,当聊天应用程序执行OnSnapShot时,QuerySnapShot返回空。
useEffect(() => {
if (!token || !eventId) return; // Add eventId check
fetchEventDetails();
console.log("Fetching messages for event:", eventId);
const messagesRef = collection(db, "events", eventId, "chats", "default", "messages");
const messagesQuery = query(messagesRef, orderBy("timestamp", "desc"));
const unsubscribe = onSnapshot(messagesQuery, querySnapshot => {
// Add a log at the start of the callback to verify it runs.
console.log("onSnapshot callback triggered");
// Check if the snapshot is empty
if (querySnapshot.empty) {
console.log("No documents found in messages");
} else {
console.log("Snapshot data:", querySnapshot.docs.map(doc => doc.data())); // Log the data
}
setMessages(
querySnapshot.docs.map(doc => ({
message_id: doc.ref.id,
user_id: doc.data().user_id,
username: doc.data().username,
content: doc.data().content,
timestamp: doc.data().timestamp ? doc.data().timestamp.toDate() : new Date(),
fileUrl: doc.data().fileUrl || "",
}))
);
});
return () => unsubscribe();
}, [token, eventId]);
FireBase之所以可以使用,是因为我可以在登录时进行验证。我的日志为:
(Nobridge)日志获取事件的消息:4(nobridge)登录onsnapshot回调触发
我可以解决这个问题? 我制作了许多控制台日志。即使我试图直接访问一个文档,但我仍然无法访问它。
(nobridge)日志在消息中找不到任何文档
interface Message {
user_id: number;
username: string;
content: string;
timestamp: Date;
animationValue?: Animated.Value;
}
useEffect(() => {
if (!token || !eventId) return; // Add eventId check
fetchEventDetails();
console.log("Fetching messages for event:", eventId);
fetchMessages();
}, [token, eventId]); const fetchMessages = async () => {
if (!user) return;
const messagesRef = collection(db, "events", "4", "chats", "default", "messages");
const messagesQuery = query(messagesRef, orderBy("timestamp", "desc"));
const unsubscribe = onSnapshot(messagesQuery, querySnapshot => {
// Add a log at the start of the callback to verify it runs.
console.log("onSnapshot callback triggered");
// Check if the snapshot is empty
if (querySnapshot.empty) {
console.log("No documents found in messages");
} else {
console.log("Snapshot data:", querySnapshot.docs.map(doc => doc.data())); // Log the data
}
setMessages(
querySnapshot.docs.map(doc => ({
user_id: doc.data().user_id,
username: doc.data().username,
content: doc.data().content,
timestamp: doc.data().timestamp ? doc.data().timestamp.toDate() : new Date(),
fileUrl: doc.data().fileUrl || "",
}))
);
});
return () => unsubscribe();
};
const renderMessage = useCallback(
({ item }: { item: Message }) => {
const isSender = item.user_id === userId;
const messageDate = item.timestamp;
return (
<Animated.View
style={[
styles.messageContainer,
isSender ? styles.sender : styles.receiver,
{ opacity: item.animationValue || 1 },
]}
>
<Text style={styles.messageText}>{item.content}</Text>
<Text style={styles.messageTime}>
{messageDate.toLocaleTimeString("en-US", {
hour: "2-digit",
minute: "2-digit",
hour12: false,
})}
</Text>
</Animated.View>
);
},
[userId]
);
打开聊天页面时,我正在接受这个。 @frankvanpuffelen也在我输入聊天页面时在日志上进行此操作。 验证工作正常,但是。 Firestore不起作用。这是我的firebaseconfig文件:
这里是一个日志:
>(NOBRIDGE) LOG onSnapshot callback triggered
>(NOBRIDGE) LOG No documents found in messages
>(NOBRIDGE) LOG onSnapshot callback triggered
>(NOBRIDGE) LOG No documents found in messages
>(NOBRIDGE)WARN [2025-02-11T17:00:55.993Z] @firebase/firestore: Firestore
> (11.3.0): WebChannelConnection RPC 'Listen' stream 0x512e1a58
> transport errored: {"defaultPrevented": false, "g": {"C": undefined,
> "F": null, "M": [Circular], "g": {"A": null, "Aa": 12, "B": 0, "C":
> null, "Ca": false, "D": "gsessionid", "Da": [Hc], "F": true, "G": 0,
> "H": [Object], "I": [T], "J": true, "K": "WFmJfthL_EMD2lhjW-y5bg",
> "L": 45000, "M": false, "O": true, "P": false, "R": 92, "S": [Object],
> "T": 0, "Ta": 5000, "U": 88681, "Ua": false, "Va": false, "W":
> "https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel",
> "Wa": 2, "X": true, "Xa": undefined, "Y": 1, "Ya": 1, "ba": true,
> "ca": undefined, "cb": 10000, "g": null, "h": [ic], "i": [Array],
> "ia": "", "j": [vb], "ja": undefined, "ka": null, "l": [Z], "la": 8,
> "m": null, "o": null, "pa": undefined, "qa": [T], "s": null, "u":
> null, "v": 0, "wa": 600000, "ya":
> "NlrUn6684y0jRyF-4aKII75hLaZwcrHBjwXwZhL3uy4", "za": -1}, "h":
> {"database": "projects/socius-0/databases/(default)"}, "i": {"g":
> [Object], "h": 4, "src": [Circular]}, "j": {"g": [Circular]}, "l":
> "https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel",
> "s": false, "u": true, "v": true}, "status": 1, "target": {"C":
> undefined, "F": null, "M": [Circular], "g": {"A": null, "Aa": 12, "B":
> 0, "C": null, "Ca": false, "D": "gsessionid", "Da": [Hc], "F": true,
> "G": 0, "H": [Object], "I": [T], "J": true, "K":
> "WFmJfthL_EMD2lhjW-y5bg", "L": 45000, "M": false, "O": true, "P":
> false, "R": 92, "S": [Object], "T": 0, "Ta": 5000, "U": 88681, "Ua":
> false, "Va": false, "W":
> "https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel",
> "Wa": 2, "X": true, "Xa": undefined, "Y": 1, "Ya": 1, "ba": true,
> "ca": undefined, "cb": 10000, "g": null, "h": [ic], "i": [Array],
> "ia": "", "j": [vb], "ja": undefined, "ka": null, "l": [Z], "la": 8,
> "m": null, "o": null, "pa": undefined, "qa": [T], "s": null, "u":
> null, "v": 0, "wa": 600000, "ya":
> "NlrUn6684y0jRyF-4aKII75hLaZwcrHBjwXwZhL3uy4", "za": -1}, "h":
> {"database": "projects/socius-0/databases/(default)"}, "i": {"g":
> [Object], "h": 4, "src": [Circular]}, "j": {"g": [Circular]}, "l":
> "https://firestore.googleapis.com/google.firestore.v1.Firestore/Listen/channel",
> "s": false, "u": true, "v": true}, "type": "c"}