Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
firestore sub collection removes change listeners when limit is reached?

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:

  • it doesn't it simply shows the 25 created.
  • it ignores any new items so the query is simply broken from that point up.

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]);

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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):

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!