Bullwinkle
Bullwinkle

Reputation: 359

showing complete firebase json data on webpage

I'm using firebase to collect data, and i'd like to create a temporary webpage to quickly see the json database (and share it) while testing. The data is grouped by a date string then the with a random key and then the data. Eg:

{
  "20160304" : {
    "-KC-aOwSWpt4dlYmjJE4" : {
      "coordinates" : "-37.7811465912404, 145.005993055861",
      "event" : "Test event",
      "time" : "2016-03-04 07:48:43 +0000"
    }, etc...

To test showing the event data I'm using javascript in the html as follows:

var myFirebaseRef = new Firebase("https://xxxx.firebaseio.com/");

myFirebaseRef.orderByKey().on("value", function(snapshot) {
  snapshot.forEach(function(snapshot) {
    console.log("The " + snapshot.key() + " event is " + snapshot.val().event);
  });
});

But it only returns The 20160304 event is undefined The 20160305 event is undefined

Does anyone know how I can grab the event string?

Upvotes: 0

Views: 222

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598718

You're trying to skip a level in your JSON:

<date>
    <pushid>
        coordinates: ...
        event: ...
        time: ...

Since you're listening on the root if this structure, your snapshot.forEach() loops over the dates. That means that you still need to loop over the push ids:

var myFirebaseRef = new Firebase("https://xxxx.firebaseio.com/");

myFirebaseRef.orderByKey().on("value", function(snapshot) {
  snapshot.forEach(function(snapshot) {
    var events = snapshot.val();
    Object.keys(events).forEach(function(key) {
      console.log(events[key]);
    });
  });
});

If on the other hand you only want to cater for one event per day, you should store them without the pushid level in there.

Upvotes: 1

Related Questions