Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!