Transcation updateFunction参数为null [英] Transcation updateFunction parameter is null

查看:151
本文介绍了Transcation updateFunction参数为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获得交易,需要一些帮助。

我使用FB应用程序面板创建了以下数据库结构:
flickering-heat- 528
测试
设置:hello

我可以在仪表板中看到数据存在:





然后我做:

  var settingsRef = new Firebase('https://flickering-heat-528.firebaseio.com/test/settings'); 
$ b settingsRef.transaction(function(json){
alert(json);
},function(error,committed,snapshot){
},true);

参见
但是警报总是'null'为什么??

Firebase中的交易回调可能会多次触发。为了理解这是为什么,我们来看看如何执行这个操作。



让我们从这个逻辑开始:如果当前值为null,则希望它变成byebye 。
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $返回$ byebye;
}
return current;
},function(error,committed,snapshot){
},true);

Firebase客户端还不知道设置的值 code> node,所以它会调用你的事务,假定当前值是 null 。然后发送给您的值和您返回到Firebase服务器的值的组合。 Firebase服务器将数据库中的当前值(hello)与它提供给您的值进行比较并确定它们不匹配(hello<> null )。因此,它会将当前值(hello)返回给客户端。


$ b Firebase客户端会调用您的回调再次。这次它传入从服务器听到的值(hello):

  settingsRef.transaction(function(current){
if(!current){
returnbyebye;
}
return current +!;
},函数(error,committed,snapshot){
},true);

现在你的函数返回hello!。 Firebase会再次发送给您的值以及您返回给服务器的值:hellohello!。如果数据库中的值仍然是hello ,它会设置您指定的新值<$ c
$ b

这就是所谓的比较 - 设置操作,它解释了为什么你的回调可能被多次调用。


I can't get transaction to work and need some help

I created the following db structure using the FB app dashboard: flickering-heat-528 test settings: "hello"

I can see in the dashboard that the data is there:

I then do:

var settingsRef=new Firebase('https://flickering-heat-528.firebaseio.com/test/settings');

settingsRef.transaction(function(json) {
    alert(json);
}, function(error, committed, snapshot) {
}, true);

see jsfiddle

But the alert is always 'null' why ??

解决方案

Transaction callbacks in Firebase may fire multiple times. To understand why that is, let's see how this operation may be executed.

Let's start with this logic: if the current value is null, you want it to become "byebye".

settingsRef.transaction(function(current) {
    if (!current) {
       return "byebye";
    }
    return current;
}, function(error, committed, snapshot) {
}, true);

The Firebase client doesn't yet know the value of your settings node, so it invokes your transaction with an assumption: that the current value is null. It then sends the combination of the value if gave to you and the value you returned to the Firebase server.

The Firebase server compares the current value in the database ("hello") against the value it provided to you and decides they don't match ("hello" <> null). So it sends the current value ("hello") back to the client.

The Firebase client invokes your callback again. This time it passes in the value it heard from the server ("hello"):

settingsRef.transaction(function(current) {
    if (!current) {
       return "byebye";
    }
    return current + "!";
}, function(error, committed, snapshot) {
}, true);

So now your function returns "hello!". Firebase again sends the value it gave you and the value you returned to the server: "hello" and "hello!". The server does the same comparison as before and if the value in the database is still "hello" it sets the new value you specified "hello!".

This is called a compare-and-set operation and explains why your callback may be called multiple times.

这篇关于Transcation updateFunction参数为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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