Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
Resetting value in firebase database

I have a problem with firebase. Everything is ok and works but if i too much call onBuyHandler function (if i click too much times buy button (because it calls this function)) the value is resetting and is setting from 1 and I don't know why. Can someone help me?

This function:

    const onBuyHandler = (event) => {
    const db = ref(database);
    const foodRef = ref(
        database,
        `users/${user.uid}/items/${props.room}/${props.item.name}`
    );
    get(foodRef)
        .then((snapshot) => {
            if (snapshot.hasChild('name')) {
                runTransaction(foodRef, (data) => {
                    if (data) {
                        data.amount++;
                        invCtx.setItems((prevState) => {
                            const newState = { ...prevState };
                            newState[props.item.name] = data;
                            return { ...newState };
                        });
                    }
                    return data;
                });
            } else {
                set(foodRef, {
                    name: props.item.name,
                    amount: 1,
                });
                invCtx.setItems((prevState) => {
                    const newState = { ...prevState };
                    newState[props.item.name] = {
                        name: props.item.name,
                        amount: 1,
                    };
                    return newState;
                });
            }
        })
        .catch((error) => {
            console.log(error);
        });
};
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!