如何从firebase中提取数据以放入crossfilter?

时间:2014-07-28 10:17:07

标签: firebase crossfilter

我想从Firebase数据库中提取数据,以便传入crossfilter。但是,我很难理解Firebase需要的结构输出数据,以便成为crossfilter的参数。

以下是从Firebase输出的数据的结构:

[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"},
    "-JStYZw6bhnFhYYwe0JI":{"FID":"quake.2013p618439","agency":"WEL(GNS_Primary)","depth":"27.0312","latitude":"-41.8992","longitude":"174.3117","magnitude":"4.6968","magnitudetype":"M","origin_geom":"POINT (174.31173 -41.899212)","origintime":"2013-08-17T19:44:28.998","phases":"130","publicid":"2013p618439","status":"automatic","type":"","updatetime":"2013-08-17T19:48:39.064"},
    "-JStY_-q5x78w9VkRfpw":{"FID":"quake.2013p618432","agency":"WEL(GNS_Primary)","depth":"5.9961","latitude":"-41.7495","longitude":"174.02","magnitude":"1.8642","magnitudetype":"M","origin_geom":"POINT (174.02 -41.749481)","origintime":"2013-08-17T19:40:45.348","phases":"11","publicid":"2013p618432","status":"automatic","type":"","updatetime":"2013-08-17T19:42:33.279"},
    "-JStY_4cCuMKZkGfgH9P":{"FID":"quake.2013p618429","agency":"WEL(GNS_Primary)","depth":"5.9375","latitude":"-41.7008","longitude":"174.0876","magnitude":"2.1629","magnitudetype":"M","origin_geom":"POINT (174.08765 -41.700788)","origintime":"2013-08-17T19:39:16.665","phases":"19","publicid":"2013p618429","status":"automatic","type":"","updatetime":"2013-08-17T19:41:32.969"}...

我使用以下代码

生成的
function PullFirebase() {
    new Firebase('https://myfirebase.firebaseIO.com/quakes').on('value', function (snapshot) {
        var Data = 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(Data);
    });
}
PullFirebase();

当我尝试将firebase上面的'Data'放入crossfilter

时,我收到此错误
[object Object](undefined) = {} 

我似乎需要将Firebase输出转换为对象字面值,因为我知道以下结构在作为firebase的参数时可以使用:

"[object Object],[object Object],[object Object],[object Object],[object Object](5) = [{"FID":"quake.2013p618454","publicid":"2013p618454","origintime":"2013-08-17T19:52:50.074","longitude":"174.1242","latitude":"-41.5396","depth":25,"magnitude":"1.7345","magnitudetype":"M","status":"automatic","phases":"17","type":"","agency":"WEL(GNS_Primary)","updatetime":"2013-08-17T19:54:11.27","origin_geom":"POINT (174.12425 -41.539614)","dtg":"2013-08-17T23:52:50.000Z","lat":-41.5396,"long":174.1242,"mag":1.7},
    {"FID":"quake.2013p618440","publicid":"2013p618440","origintime":"2013-08-17T19:45:25.076","longitude":"175.9561","latitude":"-38.8725","depth":26,"magnitude":"2.6901","magnitudetype":"M","status":"automatic","phases":"13","type":"","agency":"WEL(GNS_Primary)","updatetime":"2013-08-17T19:48:15.374","origin_geom":"POINT (175.95611 -38.872468)","dtg":"2013-08-17T23:45:25.000Z","lat":-38.8725,"long":175.9561,"mag":2.7},
    {"FID":"quake.2013p618439","publicid":"2013p618439","origintime":"2013-08-17T19:44:28.998","longitude":"174.3117","latitude":"-41.8992","depth":27,"magnitude":"4.6968","magnitudetype":"M","status":"automatic","phases":"130","type":"","agency":"WEL(GNS_Primary)","updatetime":"2013-08-17T19:48:39.064","origin_geom":"POINT (174.31173 -41.899212)","dtg":"2013-08-17T23:44:28.000Z","lat":-41.8992,"long":174.3117,"mag":4.7},
    {"FID":"quake.2013p618432","publicid":"2013p618432","origintime":"2013-08-17T19:40:45.348","longitude":"174.02","latitude":"-41.7495","depth":6,"magnitude":"1.8642","magnitudetype":"M","status":"automatic","phases":"11","type":"","agency":"WEL(GNS_Primary)","updatetime":"2013-08-17T19:42:33.279","origin_geom":"POINT (174.02 -41.749481)","dtg":"2013-08-17T23:40:45.000Z","lat":-41.7495,"long":174.02,"mag":1.9},
    {"FID":"quake.2013p618429","publicid":"2013p618429","origintime":"2013-08-17T19:39:16.665","longitude":"174.0876","latitude":"-41.7008","depth":6,"magnitude":"2.1629","magnitudetype":"M","status":"automatic","phases":"19","type":"","agency":"WEL(GNS_Primary)","updatetime":"2013-08-17T19:41:32.969","origin_geom":"POINT (174.08765 -41.700788)","dtg":"2013-08-17T23:39:16.000Z","lat":-41.7008,"long":174.0876,"mag":2.2}]

1 个答案:

答案 0 :(得分:2)

一个简单的例子,使用您在问题中使用的on('value',方法:

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);
    console.log(xf.groupAll().reduceCount().value());
    var quakesByMagnitude = xf.dimension(function(q) { return q.magnitude; });
    quakesByMagnitude.filterRange([1,2]); // filters the original list
    console.log(xf.groupAll().reduceCount().value());
});

这个小提琴显示了这种方法:http://jsfiddle.net/SkW6T/

请注意,我对您的其他问题提供了类似的答案:How to pull only the nodes/values from firebase and not the keys?。在那里,我还解释了另一种方法,您可以在其中监视quakes列表中的更改,而不是在每次更改时迭代所有子项。