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

274
Vistas
How do I stop listening to database changes in Firebase Firestore?

I have a web application that is built in Nextjs and uses Firebase Firestore for storing data. Everything else is fine with the app, but tools that test website speed give it horrible grades, because requests to Firestore never finish even though the app has already gotten all the data it needs on the frontend.

My code for fetching data:

const getData = () => {
        const q = query(collection(firestore, "data"), where("published", "==", true));
        const data = [];
        const unsubscribe = onSnapshot(q, (querySnapshot) => {
          querySnapshot.forEach((doc) => {
            data.push({id: doc.id, ...doc.data()});
          });
        });

        return data
}

The function for returning data works fine and everything is returned quickly, but the request keeps hanging without finishing. I've attached a screenshot captured from Chrome devtools. Image: "CAUTION: request is not finished yet!" How do I get Firestore to stop listening for changes in the database?

I've tried looking for solutions everywhere online and found some people saying that using "const unsubscribe = ..." to define the query and later calling "unsubscribe();" would work, but calling "unsubscribe();" prevents the function from returning the data at all for some reason.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you want to fetch data only once, then use getDocs() instead:

const getData = async () => {
  const q = query(collection(firestore, "data"), where("published", "==", true));
  const querySnapshot = await getDocs(q);
  return querySnapshot.docs.map(doc => ({id: doc.id, ...doc.data()}))
}

Unlike onSnapshot(), getDocs() won't listen for realtime updates. You can just call getData() again to reload the data. You can learn more about fetching data from Firestore in the documentation.

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