skip to Main Content

I have this issue and can’t figure out how to solve it, addDoc() and setDoc() functions separately without using "if" "else" work correctly, but when I use "if" "else" it gives me this error.

"Error adding product to cart: TypeError: Cannot read properties of undefined (reading ‘quantity’)"

It is a function to add a product to the cart, it must check if the document is already in the subcollection, then increase quantity +1, otherwise create a document, taking the name of the product as the document id.

I’am using Vue.js 3 and Firebase 9.

const productRef = doc(db, "carts", cartId, "cartProducts", this.productName);
                        const productDoc = await getDoc(productRef);
    
                        if (productDoc.exists) {
                            await updateDoc(productRef, {
                                quantity: productDoc.data().quantity + 1
                            });
                            
                        } else {
                            await setDoc(productRef, product);
                        }

2

Answers


  1. Chosen as BEST ANSWER

    solved using runTransaction

    import { runTransaction } from "firebase/firestore";
    const productRef = doc(db, "carts", cartId, "cartProducts", this.productName);
    
                        try {
                            await runTransaction(db, async (transaction) => {
                                const newQ = await transaction.get(productRef);
                                if (!newQ.exists()) {
                                    await setDoc(productRef, product);
                                    throw " Document no exist ";
                                }
                                const newQuant = newQ.data().quantity + 1;
                                transaction.update(productRef, { quantity: newQuant });
    
                            });
                            console.log("Transaction successfully committed!");
                        } catch (e) {
                            console.log("Transaction failed: ", e);
                        }
    

  2. The error

    Error adding product to cart: TypeError: Cannot read properties of
    undefined (reading ‘quantity’)

    indicates that the field quantity is undefined in the productDoc.

    So either you enforce through security rules that quantity has a correct value when the productDoc doc is created/updated or you adapt your code to handle this case as follows, for example:

    const productRef = doc(db, "carts", cartId, "cartProducts", this.productName);
    const productDoc = await getDoc(productRef);
    console.log(JSON.stringify(productDoc.data()));
    if (productDoc.exists && productDoc.data().quantity) {
        await updateDoc(productRef, {
            quantity: productDoc.data().quantity + 1
        });
    
    } else {
        await setDoc(productRef, product);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search