Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase - Get data by key or chid values - javascript

I am trying to read the data from firebase database, and display the same in a webpage.

My database structure is as below - Image here

If you see the image, i am able to read the "UserData" using the below code -

firebase.initializeApp(config);
var database = firebase.database(); 

var ref = database.ref('UserData');
ref.once('value', gotData1, errData);

    function gotData1(data){
    //console.log(data.val());
    var usrData = data.val();
    var keys = Object.keys(usrData);
    //console.log(keys);

    for (var i = 0; i< keys.length; i++){
        var k = keys[i];
        var id = usrData[k].AssignedID;
        var name = usrData[k].Name;

        $(document).ready(function() {
             var $formrow = '<tr><td>'+id+'</td><td>'+name+'</td></tr>';
             $('#userInfo').append($formrow);
        });
     }
    }

In the highlighted part of the image, you can see keys with values 196214, 196215, 196216

Now, I need to fetch the values for "One, Count" by matching the key values with available AssignedID.

How can i achieve the same?

Update, JSON as text -

{
  "app_url" : "https://app_name?ls=1&mt=8",
  "UserData" : {
    "HNpTPoCiAYMZEeVOs01ncfGBj6X2" : {
      "Name" : "Arunima Vj"
      "Email" : "[email protected]",
      "AssignedID" : 196214
    },
    "VXU2tdGdzZX90PJa9mpEL3zAiZo2" : {
      "Name" : "Lakshman Medicherla"
      "Email" : "[email protected]",
      "AssignedID" : 196215
    },
    "dFlwtqDNrja2RkOySVtW106IQP62" : {
      "Name" : "Prashanth Sripathi"
      "Email" : "[email protected]",
      "AssignedID" : 196216
    }
  }
  "teams" : {
    "196214" : {
      "1105" : {
        "One" : 7619,
        "count" : 24
      },
      "1379" : {
        "Two" : 7145,
        "count" : 21
      }
    },
    "196215" : {
      "1111" : {
        "One" : 7779,
        "count" : 20
      },
      "1508" : {
        "Two" : 1176,
        "count" : 21
      }
    },
    "196216" : {
      "1106" : {
        "One" : 7845,
        "count" : 22
      },
      "1509" : {
        "Two" : 1156,
        "count" : 26
      }
    }
  }
}
like image 953
Prashanth kumar Avatar asked Mar 16 '17 03:03

Prashanth kumar


People also ask

Is Firebase key value?

Firebase is categorized as a NoSQL database program, which stores data in JSON-like documents. In Firebase, a document is a set of key-value pairs defined by a schema.

What is getKey () in Firebase?

public String getKey () Returns. The key name for the source location of this snapshot or null if this snapshot points to the database root.

Does Firebase have primary key?

This chat application allows users to store the basic profile and contact list. The user profile would be located on a path such as Users/$uid. User is a node in it and will have a sort of primary key associated with an ID. So, we can access each one uniquely.

How do I get particular data from Firebase?

Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes. An event listener may receive several different types of events.


1 Answers

Your data structure is quite nested, which makes the code more difficult to read. But this navigates the structure generically in the minimum code I could come up with:

var ref = firebase.database().ref("/42824688");

ref.child("UserData").once('value', gotUserData);

function gotUserData(snapshot){
  snapshot.forEach(userSnapshot => {
    var k = userSnapshot.key;
    var id = userSnapshot.val().AssignedID;
    var name = userSnapshot.val().Name;
    ref.child("teams").child(id).once("value", teamsSnapshot => {
      teamsSnapshot.forEach(teamSnapshot => {
        var teamKey = teamSnapshot.key;
        teamSnapshot.forEach(teamProp => {
          var prop = teamProp.key;
          var val = teamProp.val();
          console.log(k+" "+name+" "+id+": "+teamKey+", "+prop+"="+val);
        });
      });
    });
  })
}

So for each user, this loads the teams data for that user and then loops over the teamsSnapshot to get each teamSnapshot and then loops over that to get each team property.

Working jsbin: http://jsbin.com/noziri/edit?html,js,console

like image 110
Frank van Puffelen Avatar answered Nov 11 '22 13:11

Frank van Puffelen