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

122
Visualizações
Separate firestore queries in the same react function

In the fetchUserCart() function, I am trying to first get an array containing strings with the product unique name and then set my state array with the result from the second query (query based on first query's result). While the first one works, the second one doesn't and I dont understand why. When I try to map currentProducts to render a table, the array is empty.

export const CartPage = () => {


    const [user,  error] = useAuthState(auth);
  const [cartItems, setCartItems]=React.useState(0);
  const [currentProducts, setCurrentProducts]=React.useState([]);


  const fetchUserCartItems=async()=>{
  
    const q = query(collection(db, "users"), where("uid", "==", user?.uid));
    const doc = await getDocs(q);
    const data = doc.docs[0].data();
    let cartItemsClone=data.cartItems;
    setCartItems(cartItemsClone);  
    
} 

const fetchUserCart = async() =>{
    const q = query(collection(db, "users"), where("uid", "==", user?.uid));
      const doc = await getDocs(q);
      const data = doc.docs[0].data(); 
      let cart=data.cart; 
      console.log(cart);
      let products =[];
      cart.forEach(async(item)=>{
        const q = query(collection(db, "products"), where("uniqueName", "==", item));
        const doc = await getDocs(q);
        const data = doc.docs[0].data();  
        products.push(data);
        
      })
      setCurrentProducts(products);
  }

 
React.useEffect(() => {
  fetchUserCartItems();
  fetchUserCart();
}, [user], [currentProducts] );
about 4 years ago · Juan Pablo Isaza
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