Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda