How to increase a record in Firebase?

I have a Firebase record "search: 0" for each user. In some event, I would like to add 1 to the current account. I got this far, but for some reason it doesn't work:

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 make this work?

Thanks.

+7
javascript firebase firebase-database
source share
2 answers

You can use transaction

 var databaseRef = firebase.database().ref('users').child(user_uid).child('searches'); databaseRef.transaction(function(searches) { if (searches) { searches = searches + 1; } return searches; }); 
+14
source share
 ref.transection(function(queue_length) { if(queue_length|| (queue_length === 0)) { queue_length++; } return queue_length; }) 

You can use it like this :)

0
source share

All Articles