0

I know that arrayUnion doesnt accept duplicates, but is there anyway I could prevent that and update just one field (quantity) if the item is duplicate? Maybe you have another idea of how to structure my data in order to achieve this if a solution for this doesnt exist. Thank you

Here is my db: firestore

and here is the function where I try to do that (ofc this doesnt work because async):

 const addProductToCart=async()=>{

   const q = query(collection(db, "carts"), where("userUID", "==", user?.uid));
      const docRef = await getDocs(q);
      if(docRef.docs[0].exists){
         console.log('not empty');

         let products=docRef.docs[0].data().products;
         products.forEach((product)=>{
           //product exists
           if(product.productUniqueName == uniqueProductName){
            //console.log(product)
            let cloneProduct=product;
            let updateProduct=product;
            updateProduct.quantity=product.quantity+1;
            console.log('Updated product'+updateProduct);
            
            const currentUserCart=doc(db, 'carts', docRef.docs[0].id);
             updateDoc(currentUserCart, {
            cartItems: cartItems + 1,
            products: arrayRemove(cloneProduct),
            products: arrayUnion(updateProduct)
         });
           return;
           }
          })


        //productUniqueName
         let newProduct={};
         newProduct.productName=product.name;
         newProduct.productBrand=product.brand;
         newProduct.price=product.price;
         newProduct.size=product.quantity;
         newProduct.quantity=1;
         newProduct.productUniqueName=uniqueProductName;
         newProduct.img=productImages[0];

         const currentUserCart=doc(db, 'carts', docRef.docs[0].id);
         await updateDoc(currentUserCart, {
         cartItems: cartItems + 1,
         products: arrayUnion(newProduct)
       });

      }
      else{
        console.log('empty');
      }
    }
3
  • a tip: never let the user send a price. make cloud function read the price from the item on checkout and make sure that the users only can READ the products and not modify them with security rules. Commented Apr 18, 2022 at 6:24
  • Also, the problem I see here is that you don't have a unique ID for each of the items in your basket. You just add a product without any ID to it. You might want to read up on the structure your data firebase.google.com/docs/firestore/manage-data/… Commented Apr 18, 2022 at 6:27
  • I kinda do that with productUniqueName Commented Apr 18, 2022 at 10:17

2 Answers 2

2

There isn't any direct way to do so. You'll have to read the document, check if the item already exists in that array, if it does then increment quantity and update the whole array back. If not then you can add it in the array.

I'm not sure if you fetch existing cart information to show that to user. If you do so on initial load then you can check it directly from local state (where Cart info is stored) and update accordingly.

Sign up to request clarification or add additional context in comments.

2 Comments

That's what I am trying to do inside the function but what it does is: 1) adds same product with quantity=1, 2) adds same product with quantity=2
@ALL can you try let updateProduct= {...product}
0

Ok so, I think I found an workaround: here is the code for the function and it works as expected:

const addProductToCart=async()=>{

   const q = query(collection(db, "carts"), where("userUID", "==", user?.uid));
      const docRef = await getDocs(q);
      if(docRef.docs[0].exists){
         console.log('not empty');


         //added boolean to check if product exists/or not
         let productFound=false;
         let products=docRef.docs[0].data().products;

         let currentCartItems=docRef.docs[0].data().cartItems;
         products.forEach((product)=>{
           //product exists          
           
           if(product.productUniqueName == uniqueProductName){
            productFound=true;
            let updateProduct={...product};
            updateProduct.quantity=product.quantity+1;
            console.log('Updated product'+updateProduct);
            
            const currentUserCart=doc(db, 'carts', docRef.docs[0].id);
            
//did Union and Remove separately
         updateDoc(currentUserCart, {
          cartItems: currentCartItems + 1,
          products: arrayRemove(product)
       });

       updateDoc(currentUserCart, {
          products: arrayUnion(updateProduct)
     });
           //return;
           }
          })


        //productUniqueName
        if(productFound==false){
          console.log('product Not found')
          let newProduct={};
          newProduct.productName=product.name;
          newProduct.productBrand=product.brand;
          newProduct.price=product.price;
          newProduct.size=product.quantity;
          newProduct.quantity=1;
          newProduct.productUniqueName=uniqueProductName;
          newProduct.img=productImages[0];
           console.log('New Product', newProduct);
          const currentUserCart=doc(db, 'carts', docRef.docs[0].id);
          await updateDoc(currentUserCart, {
          cartItems: cartItems + 1,
          products: arrayUnion(newProduct)
        });
        }
         
       setCartItems(cartItems+1);
      }
      else{
        console.log('empty');
      }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.