0

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jay
  • 1
  • 1
    DB operations are asynchronous, which is why you have to pass a callback function in the first place. `local_qty=qty+1;` happens *before* `qty=snapshot.data().loc_qty;`. You can either put each subsequent command inside the callback function, including the code to update the DB, or you use a Promise based API where you can `await` DB operations. –  Aug 06 '22 at 13:38
  • 1
    Duplicate: [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) –  Aug 06 '22 at 13:50
  • Sorry,I am very new to this.. I am figuring out this tuff. Could anyone by the way could just modify my code using a callback function , I know i am asking too much.. But it would help me a lot not only in this code but also i could understand this stuff.. if anyone can, just help me .. i would be so glad – Jay Aug 06 '22 at 14:59
  • It's not hard. Currently your first callback only contains `qty = snapshot.data().loc_qty;` Just move the rest of the code below that line, inside the function. –  Aug 06 '22 at 15:04
  • you mean inside the "onSnapshot" ?.. it actually doesn't allow me – Jay Aug 06 '22 at 15:12
  • hey,i don't think that is the actual problem here.. it won't update even if i update it with a static value or when i intialize the local_qty with 1 or 2 – Jay Aug 06 '22 at 16:09
  • `it actually doesn't allow me` what do you mean by that? Also, add console.log()s to your code to find out which part runs. The DB update is inside an if block so maybe the test fails. –  Aug 07 '22 at 09:13
  • sorry, I actually got over the limits of the writes of firebase yesterday, so both of these added up and it just got messed up totally.. yeah i couldn't read as the render just doesn't wait for it . i also couldn't add the below code inside the update , like you stated that there is an if statement inside it.. so what should I do – Jay Aug 07 '22 at 12:14
  • Try this: https://pastebin.com/HawQk6LJ –  Aug 07 '22 at 12:18

0 Answers0