Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda