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

185
Views
Cleaning up a snapshot with firestore

I am currently using firestore/firebase for my BAAS and am working on getting some data back from a collection. I have the data rendering and communicating properly however I cannot figure out how to clean up my effect hook properly.

I've read that firestore has a built in way to do it with the onsnapshot function but I cannot figure it out.

Here is the actual API call to the firestore database:

  getBuds: async (uid) => {
    try {
      const snapshot = await db
        .collection('users')
        .doc(uid)
        .collection('buds')
        .get();
      return snapshot.docs.map((doc) => doc.data());
    } catch (error) {
      console.log('Error @getBuds ', error);
    }
  },

And here is the effect function where it's called:

useEffect(() => {
    firebase
      .getBuds(user.uid)
      .then((res) => setBuds(res))
      .catch((err) => console.log(err));
  }, [buds]);

Please help me with my cleanup!!! I would really appreciate it

UPDATE** So I have modified the original code using the onsnapshot function and am managing to pull down the data I want from fire store, I am getting each document and pushing each one into an array with this code. However I cant seem to now get this code into the component where I call the "getBuds" method :(

Here is the updated getBuds:

getBuds: async (uid) => {
    let unsubscribe;

    try {
      const budData = [];

      unsubscribe = await db
        .collection('users')
        .doc(uid)
        .collection('buds')
        .onSnapshot((snapshot) => {
          snapshot.forEach((doc) => {
            budData.push(doc.data());
          });
          console.log(budData);
          return budData;
        });
    } catch (error) {
      console.log('Error @getBuds ', error);
    }
  },
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use the unsubscribe method from a onSnapshot listener:

let unsubscribe;

getRealtimeUpdates = function(document) {
        unsubscribe = firestore.collection("collection_name")
            .onSnapshot(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {
                if (doc && doc.exists) {
                    const myData = doc.data();
                    // DO SOMETHING
                }
            });
        });
    }
  
  // unsubscribe:
  
  unsubscribe();

For more info, please look at Firestore unsubscribe to updates

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!