I am trying to increment a monetary value within firebase. When a user makes a project donation by paypal, a project key variable is attached to the paypal button and then is passed along with the transaction details to firebase. Then the mc_gross value (amount donated) is added to the amountFundraised key for that specific project key. Right now it is just replacing the current value from amountFundraised with mc_gross instead of incrementing.
So if the current amountFundraised is 100 and mc_gross is 50, the value for amountFunraised will now be 150.
Does anyone have any suggestions?
I tried using the increment() function but it is not working.
import { Router } from "express";
import { database,} from "../../firebase";
import { increment } from 'firebase/database';
export const router = Router();
router.get('/', (req, res) => {
res.send('Express + TypeScript Server');
});
router.post('/payments', (req, res) => {
console.log(req.body);
console.log(req.query);
const data = {
...req.body,
...req.query,
}
database.ref().child('/payments').push(data);
database.ref().child(`projects/${data.projectKey}`).update({amountFundraised : increment(Number(data.mc_gross))})
res.json('Okay');
})
I have also tried firebase.firestore.FieldValue.increment(Number(data.mc_gross))