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

177
Vistas
Array that is filled during a loop becomes empty after a loop Firebase

I am getting the snapshots in the cartItems collection from the Firebase and for each cartItem i am accessing the ticket_id field that assocciates the cartItem with the ticket. Then, for each ticket that is accessed, i want to get the fields in it + access the information about the Event associated with a ticket by the event_id field in a ticket. After having all the information for the ticket and the event i want to create a new array that merges all the information together(ticket information and event name from the event). Just look at the database structure for more understanding: https://i.stack.imgur.com/9IzoG.png, https://i.stack.imgur.com/XiaxX.png, https://i.stack.imgur.com/V5FOh.png

this is how i do it in the react native code:

    useEffect(() => {
        const subscriber = onSnapshot(q, (snapshot) => {
            let cartItems = [];
            snapshot.docs.forEach((cart_item) => {
                getDoc(doc(getFirestore(), 'tickets', cart_item.data().ticket_id)).then((ticket_item) => {
                    getDoc(doc(getFirestore(), 'events', ticket_item.data().event_id)).then((event_item) => {
                        cartItems.push({...ticket_item.data(), event_name: event_item.data().name, id: cart_item.id});
                    });
                });
            });
            console.log(cartItems);
        })
        return () => subscriber();
    }, [])

However, in the place i am logging the array, it is empty. If i log the array just after cartItems.push(...), then it is filled very well during the triple loop. Why is it so? Maybe you even have a better solution to structure the database and access the information of the items?

about 4 years ago · Juan Pablo Isaza
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