When data is added to a subcollection the real-time data snapshot is not getting triggered if the limit I set is reached. How to go around this?
I want the snapshot to display the last 25 newest texts. But since the limit is reached it just halts the function and I am stuck with the last 25 rendered and new ones don't show at all.
What is even worst about this if I reload the page to see if the function will render the new items after the limit has reached:
useEffect(() => {
if (_globalContext.focus) {
let collectionRef = collection(db, 'Chat', docId, 'messages');
const querydata = query(collectionRef, orderBy('createdAt'), limit(25));
const unsub = onSnapshot(querydata , (querySnapshot) => {
console.log(querySnapshot, 'triggered');
querySnapshot.forEach((doc) => {
console.log(doc.data());
});
});
setChats(data);
return () => {
unsub();
};
}
}, [_globalContext.focus]);
If you want to show the 25 most recent documents, you need to either sort on descending timestamp or limit to the *last 25 (instead of the first 25):