I'm looking at the firestore docs for hours and still didnt found solution for this case. I need to add an ammout feature to product my E-commerce app.
data structure : the main collection is "cart" and the doc is the user email.
here is the current code for add or set a product:
import firebase from 'firebase';
export async function addToCart(userMail, itemId, name, url, price, category, type, description) {
const ammout = 1
const cartItem = { itemId, name, url, price, category, type, description, ammout }
if (userMail === undefined) throw console.error('please Login');
const userDoc = await firebase.firestore().collection("cart").doc(userMail)
await userDoc.get().then((doc) => {
if (doc.exists) {
(async () => {
await userDoc.update({
item: firebase.firestore.FieldValue.arrayUnion(cartItem),
})
})()
}
else {
(async () => {
await userDoc.set({
item: firebase.firestore.FieldValue.arrayUnion(cartItem)
})
})()
}
})
}