Firebase交易的奇怪行为 [英] Strange behaviour of firebase transaction

查看:41
本文介绍了Firebase交易的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的firebase看起来像这样:

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
  )

并输出:

child_added https://my_firebase.firebaseio.com/items/item1 { my_key: 'my_val' }
transaction on data null
commited? false
server data null

transaction(...)的第三个参数为true时,也会发生同样的情况. 为了使此代码正常工作,我必须将ref.once 'child_added', (snapshot) ->更改为ref.on 'child_added', (snapshot) ->(将once更改为on).更改后的输出为:

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' }

似乎由于某种原因,当我使用once时,数据未正确同步且本地快照未更新,并且事务认为"引用下没有数据.是错误还是我做错了什么?我知道updateFunction可以被多次调用的事务,以及第三个参数(我已经尝试过使用true和false选项),但是我仍然不明白为什么使用once获取事务时为什么事务不起作用一个孩子.

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.

推荐答案

事务最终应成功,并以正确的数据状态运行,但最初将以未缓存"状态运行,这意味着它将针对客户的数据本地副本(可能是null),尝试将更改提交到服务器(这将失败),然后重试事务.

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.

这是正常,并且是预期的.但是,如果交易没有成功,我建议您通过support@firebase.com与支持人员联系,以继续解决问题.

This is normal, and expected. If, however, the transaction does not ever succeed, I would recommend reaching out to the support folks at support@firebase.com to continue troubleshooting the problem.

这篇关于Firebase交易的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆