I have a collection for user's cart items. I'm trying to copy them to orders collection once the user successfully checks-out so that I can delete the cart collection. However, I keep getting an error message saying "enter image description here". Below is my code to query "orders" collection using product id (which is configured to be the document ID for each cart doc and is working as intended).
const getOrders = async (productData) => {
const cartCollection = await collection(db, "users", currentUser.id, "cart");
const cartQuerySnapshot = await getDocs(cartCollection);
const orderCollection = await collection(db, "users", currentUser.id, "orders")
const orderQuerySnapshot = await getDocs(orderCollection)
const orderRef = doc(db, "users", currentUser.id, "orders", productData.id);
const orderDoc = await getDoc(orderRef);
if (!orderDoc.exists()) {
await setDoc(orderRef, { quantity: 1 } );
} else {
// increment the product's quantity by 1
let quantity = orderDoc.data().quantity;
quantity++;
// If the document already exists, increment value based on quantity and merge
await updateDoc(
orderRef,
{ quantity: Number(quantity) },
{ merge: true }
);
}
I'm able to query the cart documents and console log their id and quantity using the following:
cartQuerySnapshot.forEach((doc) => {
console.log(doc.id, doc.data().quantity);
});
Is there a way I can assign id and quantity of cart docs to order docs in the code block above. I apologize if my question is not clear, this is my first time posting and any feedback is appreciated. Thank you :)