1

I'm trying to increment a counter value in my Firebase database using transactions. it works the first time, i.e. when the value is set to 1, but on updates after that, the value isn't updating.

Here's how my code:

firebaseRef.child("search_hashtags").child(hashtag).transaction(function(currentValue) {
    return (currentValue || 0) + 1;
}, function(error, committed, ss) {
    console.log(error);
    console.log(committed);
    console.log(ss);
});

The error I log says "[Error: set]"

I turned on Firebase logging and I'm getting this:

p:0: from server: {"r":3,"b":{"s":"ok","d":""}}  
p:0: p response {"s":"ok","d":""}  
p:0: handleServerMessage d {"p":"search_hashtags/test","d":1}  
p:0: from server: {"r":4,"b":{"s":"ok","d":{}}}  
p:0: listen response {"s":"ok","d":{}}  
p:0: from server: {"r":5,"b":{"s":"datastale","d":"Transaction hash does not match"}}  
p:0: p response {"s":"datastale","d":"Transaction hash does not match"}  
0: transaction put response {"path":"/search_hashtags/test","status":"datastale"} 

I'm doing similar transactions on other nodes in other parts of my code and they work fine, but for some reason this one fails...

rodskagg
  • 3,827
  • 4
  • 27
  • 46
  • Are you changing the data under `search_hashtags/test` (or a lower node) with a regular write (e.g. `set()` or `update()`) around the same time? – Frank van Puffelen Jan 24 '16 at 23:30
  • No, this is the only time I'm doing anything with search_hashtags. I'm doing a lot of writes at the same time, however, but to other nodes in other branches of the node tree. – rodskagg Jan 25 '16 at 08:16
  • @FrankvanPuffelen I'm running Node 4.2.4, but the way. if that helps... – rodskagg Jan 25 '16 at 15:09
  • Note that `datastale` is not a failing transaction. It just meant that the data Firebase gave your callback didn't match the value on the server. So the server returns the current value and your callback will be invoked again. Unless you're seeing an error in the second callback, you can safely ignore these. – Frank van Puffelen Jan 25 '16 at 18:08
  • See these answers for some information on how Firebase transactions work: http://stackoverflow.com/questions/34804129/why-is-the-currentdata-in-my-firebase-transaction-always-nil/34804228#34804228, http://stackoverflow.com/questions/33578887/transcation-updatefunction-parameter-is-null/33578953#33578953 – Frank van Puffelen Jan 25 '16 at 18:11

0 Answers0