I have a Firebase record \"searches: 0\" for each user. On some event, I\'d like to add 1 to what the current count is. I\'ve gotten this far, but for some reason, it\'s not
There's a new method ServerValue.increment()
in firebase JavaScript SDK v7.14.0
It's better for performance and cheaper since no round trip is required.
See here
Added ServerValue.increment() to support atomic field value increments without transactions.
API Docs here
Usage example:
firebase.database()
.ref('users')
.child(user_uid)
.child('searches')
.set(firebase.database.ServerValue.increment(1))
Or you can decrement, just put -1
as function arg like so:
firebase.database()
.ref('users')
.child(user_uid)
.child('searches')
.set(firebase.database.ServerValue.increment(-1))