I am trying to update a value in the firestore but it isn't working. I could retrieve the same data from the firebase but just couldn't update it. In the same way i have updated another document in another page but this isn't updating. I have almost thought of it in all different way but could not get the solution. How it should work: i retrieve a number (loc_qty) from the firestore and i should increment and store it back in the same field
How actually it works: i am able to retrieve a number (loc_qty) from the firestore and i am able to increment it but couldn't store back the new value that is incremented
The following is the corresponding code:
const [{ basket, user }, dispatch] = useStateValue();
var qty = 0;
var local_qty = 0;
const addtoBasket = () => {
db.collection('users').doc(user.uid).collection('products_loc_qty').doc(props.id).onSnapshot((snapshot) => {
qty = snapshot.data().loc_qty;
})
local_qty = qty + 1;
if (props.quantity >= local_qty) {
console.log(local_qty, props.id, user.uid)
db.collection('users').doc(user.uid).collection('products_loc_qty').doc(props.id).update({
loc_qty: parseInt(local_qty),
})
dispatch({
type: 'ADD_TO_BASKET',
item: {
id: props.id,
title: props.title,
price: props.price,
rating: props.rating,
image: props.image,
quantity: 1
}
});
}
}
I would be very glad if anyone could help me out .. stuck here all evening