Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
111
rated 0 times [  114] [ 3]  / answers: 1 / hits: 20016  / 9 Years ago, wed, july 15, 2015, 12:00:00

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 working:



du.fbAddSearchCount = function() {
var usr = new Firebase(firebase_url + /users/ + user_uid);

usr.on(value, function(snapshot) {
user = snapshot.val();

var usersRef = ref.child(users);

var fbUser = usersRef.child(user_uid);
fbUser.update( {
searches: user.searches + 1
});
}
}


Any help to get this working?



Thank you.


More From » firebase

 Answers
5

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))

[#65803] Monday, July 13, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daja

Total Points: 407
Total Questions: 103
Total Answers: 103

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
daja questions
Tue, Dec 21, 21, 00:00, 2 Years ago
Thu, Apr 23, 20, 00:00, 4 Years ago
Fri, Sep 6, 19, 00:00, 5 Years ago
Tue, Jul 23, 19, 00:00, 5 Years ago
;