Reputation: 15434
My firebase looks like this:
This is test code (coffee script):
Firebase = require 'firebase'
ref = new Firebase 'https://my_firebase.firebaseio.com/items'
ref.once 'child_added', (snapshot) ->
childRef = snapshot.ref()
console.log "child_added", childRef.toString(), snapshot.val()
childRef.transaction(
(data) ->
console.log 'transaction on data', data
return if !data or data.my_key isnt 'my_val'
data.my_key = 'new_val'
return data
,
(err, commited, snapshot) ->
if err
console.error 'error', err
return
console.log 'commited? '+commited
console.log 'server data', snapshot.val()
,
false
)
And output:
child_added https://my_firebase.firebaseio.com/items/item1 { my_key: 'my_val' }
transaction on data null
commited? false
server data null
Same happens when third parameter of transaction(...)
is true.
To make this code work, I have to change ref.once 'child_added', (snapshot) ->
to ref.on 'child_added', (snapshot) ->
(once
to on
). After this change output is:
child_added https://my_firebase.firebaseio.com/items/item1 { my_key: 'my_val' }
transaction on data { my_key: 'my_val' }
commited? true
server data { my_key: 'new_val' }
It seems that for some reason when I am using once
data are not synced properly and local snapshot is not updated and transaction "thinks" that there is no data under the ref. Is it a bug or I am doing something wrong? I know about transactions that updateFunction
can be called more than one time, and about third parameter (I have tried true and false options for it) but still I can't understand why transaction does not work when using once
to obtain a child.
Upvotes: 2
Views: 1896
Reputation: 13266
The transaction should eventually succeed, and run on the correct state of the data, but will initially run in an "uncached" state, meaning it will run against the client's local copy of the data (likely to be null
), try to commit the change to the server (which will fail), and then re-try the transaction.
This is normal, and expected. If, however, the transaction does not ever succeed, I would recommend reaching out to the support folks at [email protected] to continue troubleshooting the problem.
Upvotes: 5