0

I have a question. I want to subtract A and B. A is data in firebase firestore. B is data in react native app. Here is my code.

const purchaseStory = async () => {
    if (rabbit.rabbitAmount < price) {
      console.log('impossible');
    } else {
      console.log('do you want to buy?');

      //console.log(amount);
      setRabbitAmount({
        id: user.id,
        price, **// is B**
      });
    }
  };
export function setRabbitAmount({id, price}) {
  return userRabbitsCollection.doc(id).update({
    rabbitAmount: rabbitAmount - price, **// is A**
  });
}

current state: A is 100 / B is 35 expect result: A is changed to 65 current result: NaN

what should I do if I get the expected result?

I try to change the code

export function setRabbitAmount({id, price}) {
  return userRabbitsCollection.doc(id).update({
    rabbitAmount: rabbitAmount - price, **// is A**
  });
}

--> rabbitAmount: this.rabbitAmount-price

but the result is still NaN

Sidou Gmr
  • 138
  • 8
  • Are you looking for [How to decrement integer in Firestore](https://stackoverflow.com/questions/55621632/firestore-how-to-decrement-integer-value-atomically-in-database)? – Dharmaraj Oct 28 '22 at 17:18
  • Yes absolutely but I didn't find the answer. However I found another way. I withdraw a data using get- function. And then get the amount data from firestore. and then I send the amount parameter. like this anyway finally I can get the result! export function setRabbitAmount({id, price, rabbitAmount}) { return userRabbitsCollection.doc(id).update({ rabbitAmount: (rabbitAmount = rabbitAmount - price), }); } – user551352 Oct 28 '22 at 17:38

0 Answers0