如何仅从firebase中拉取节点/值,而不是按键? [英] How to pull only the nodes/values from firebase and not the keys?

查看:158
本文介绍了如何仅从firebase中拉取节点/值,而不是按键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何只使用firebase中的节点,而不是使用javascript?换句话说,我只想要从下面的firebase的键值对的值,这意味着我不想要下面的唯一键,而是下面的。





目前,我的代码是..

  function PullFirebase(){
new Firebase('https://myfirebase.firebaseIO.com/quakes').on('value',function(snapshot){
var S = snapshot.val();

函数printData(data){
var f = eval(data);
console.log(data +(+ f.length +)=+ JSON.stringify(f)替换(/} \,/ g,},\\\
\t)。replace(],\\\
])) ;
}
printData(S);
});
}
PullFirebase();

在控制台中产生以下内容

  [object Object](undefined)= {-JStYZoJ7PWK1gM4n1M6:{FID:quake.2013p618454,agency:WEL(GNS_Primary),depth 24.5703,纬度: -  41.5396,经度:174.1242,量纲:1.7345,magnitudetype:M,origin_geom:POINT(174.12425 -41.539614) origintime: 2013-08-17T19:52:50.074, 阶段: 17, publicid: 2013p618454, 状态: 自动, 类型: , 注册 :2013-08-17T19:54:11.27},
-JStYZsd6j4Cm6GZtrrD:{FID:quake.2013p618440,agency:WEL(GNS_Primary),depth 26.3281,纬度: - 38.8725,经度:175.9561,大小:2.6901,magnitudetype:M,origin_geom:POINT(175.95611 -38.872468) origintime : 2013-08-17T19:45:25.076\" , 阶段: 13, publicid: 2013p618440, 状态: 自动, 类型: , 注册: 2013-08-17T19:48:15.374},...

但我想只能有字典,例如

  [{FID:quake.2013p618454,agency:WEL(GNS_Primary),depth:24.5703,latitude -41.5396,经度:174.1242,量纲:1.7345,magnitudetype:M,origin_geom:POINT(174.12425 -41.539614),origintime:2013-08 -17T19:52:50.074\" , 阶段: 17, publicid: 2013p618454, 状态: 自动, 类型: , 注册:2013-08-17T19: 54:11.27 },{ FID : quake.2013p597338\" , 代理: WEL(GNS_Primary), 深度: 5.0586, 纬度:  -  37.8523, 经度: 176.8801,幅度:2.2362,magnitudetype:M,origin_geom:POINT(176.88006 -37.852307),origintime:2013-08-10T00:21:54.989 :17,publicid:2013p597338,status:automatic,type:,updatetime:2013-08-10T03:42:41.324} b $ b  


解决方案

如果我正确理解你,你想得到所有的孩子



您通常有两种方法:


  1. 获取父代号的值de和循环

  2. 监视器,因为孩子被添加/更新/删除到父节点

您的方法与#1匹配,所以我先回答一个。我还会举个例子,方法#2,这是更有效的,当你的数据集改变。



迭代一个Firebase的孩子ref



在您的 on('value',处理程序中,您可以使用 forEach


$ b $新的Firebase('https://myfirebase.firebaseIO.com/quakes').on('value',function(snapshot){
var quakes = [ ];
snapshot.forEach(function(childSnapshot){
quakes.push(childSnapshot.val());
});
var filter = new crossfilter(quakes);

});

forEach 函数是同步的,所以我们可以等待循环完成,然后创建crossfilter。



监视Firebase ref的孩子



在这种情况下,最好的结构是:

  var quakes = new Firebase('https://myfirebase.firebaseIO.com/quakes'); 
var quakeCount = 0;
quakes.on('child_added',function(snapshot){
var quake = snapshot.val();
quakeCount ++;
console.log(quakeCount =+ quakeCount + ,FID =+ quake.FID);
});
quakes.on('child_removed',function(old_snapshot){
var quake = old_snapshot.val();
quakeCount--;
console.log(quakeCount = + quakeCount +,删除FID =+ quake.FID);
});

使用此代码构造,您正在积极监听已添加和删除的地震。您仍然需要保留所有地震数组,然后在 child_added child_changed child_removed



他们如何比较



当你第一次运行代码,对孩子的监视将导致与 on('value',构造相同的数据,但是当孩子被添加/删除后, on('value',将再次收到所有地震,而 on('child_added', on('child_removed ',只会被问及这个地震。


How do you only pull only the nodes from firebase and not the keys using javascript? In other words, I only want the values of the key-value pairs from the below firebase, which means I don't want the unique keys below but just what's underneath.

Currently, my code is..

function PullFirebase() {
    new Firebase('https://myfirebase.firebaseIO.com/quakes').on('value', function (snapshot) {
        var S = snapshot.val();

        function printData(data) {
            var f = eval(data);
            console.log(data + "(" + f.length + ") = " + JSON.stringify(f).replace("[", "[\n\t").replace(/}\,/g, "},\n\t").replace("]", "\n]"));
        }
        printData(S);
    });
}
PullFirebase();

This produces the following in the console

[object Object](undefined) = {"-JStYZoJ7PWK1gM4n1M6":{"FID":"quake.2013p618454","agency":"WEL(GNS_Primary)","depth":"24.5703","latitude":"-41.5396","longitude":"174.1242","magnitude":"1.7345","magnitudetype":"M","origin_geom":"POINT (174.12425 -41.539614)","origintime":"2013-08-17T19:52:50.074","phases":"17","publicid":"2013p618454","status":"automatic","type":"","updatetime":"2013-08-17T19:54:11.27"},
    "-JStYZsd6j4Cm6GZtrrD":{"FID":"quake.2013p618440","agency":"WEL(GNS_Primary)","depth":"26.3281","latitude":"-38.8725","longitude":"175.9561","magnitude":"2.6901","magnitudetype":"M","origin_geom":"POINT (175.95611 -38.872468)","origintime":"2013-08-17T19:45:25.076","phases":"13","publicid":"2013p618440","status":"automatic","type":"","updatetime":"2013-08-17T19:48:15.374"},...

but I'd like to only have the dictionaries , such as

[{"FID":"quake.2013p618454","agency":"WEL(GNS_Primary)","depth":"24.5703","latitude":"-41.5396","longitude":"174.1242","magnitude":"1.7345","magnitudetype":"M","origin_geom":"POINT (174.12425 -41.539614)","origintime":"2013-08-17T19:52:50.074","phases":"17","publicid":"2013p618454","status":"automatic","type":"","updatetime":"2013-08-17T19:54:11.27"},{"FID":"quake.2013p597338","agency":"WEL(GNS_Primary)","depth":"5.0586","latitude":"-37.8523","longitude":"176.8801","magnitude":"2.2362","magnitudetype":"M","origin_geom":"POINT (176.88006 -37.852307)","origintime":"2013-08-10T00:21:54.989","phases":"17","publicid":"2013p597338","status":"automatic","type":"","updatetime":"2013-08-10T03:42:41.324"}...]

解决方案

If I understand you correctly, you want to get all child objects under quakes.

You generally have two approach here:

  1. Get the value of the parent node and loop over the children
  2. Monitor as children are added/updated/removed to the parent node

Your approach matches with #1, so I'll answer that one first. I'll also give an example of approach #2, which is more efficient when your data set changes.

Iterate children of a Firebase ref

In your on('value', handler you can skip the unique IDs using forEach:

new Firebase('https://myfirebase.firebaseIO.com/quakes').on('value', function (snapshot) {
    var quakes = [];
    snapshot.forEach(function(childSnapshot) {
        quakes.push(childSnapshot.val());
    });
    var filter = new crossfilter(quakes);

});

The forEach function is sychronous, so we can simply wait for the loop to finish and then create the crossfilter.

Monitor children of a Firebase ref

In that case, the best construct is:

var quakes = new Firebase('https://myfirebase.firebaseIO.com/quakes');
var quakeCount = 0;
quakes.on('child_added', function (snapshot) {
    var quake = snapshot.val();
    quakeCount++;
    console.log("quakeCount="+quakeCount+", FID="+quake.FID);
});
quakes.on('child_removed', function (old_snapshot) {
    var quake = old_snapshot.val();
    quakeCount--;
    console.log("quakeCount="+quakeCount+", removed FID="+quake.FID);
});

With this code construct you're actively listening for quakes that are added and removed. You'll still have to keep an array of all the quakes, which you then modify in child_added, child_changed and child_removed.

How they compare

When you first run the code, monitoring for children will result in the same data as the on('value', construct. But when children are added/removed later on('value', will receive all quakes again, while on('child_added', and on('child_removed', will only be called for the quake in question.

这篇关于如何仅从firebase中拉取节点/值,而不是按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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