Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

306
Vistas
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" : "asd@upp.com",
      "AssignedID" : 196214
    },
    "VXU2tdGdzZX90PJa9mpEL3zAiZo2" : {
      "Name" : "Lakshman Medicherla"
      "Email" : "asd@upp.com",
      "AssignedID" : 196215
    },
    "dFlwtqDNrja2RkOySVtW106IQP62" : {
      "Name" : "Prashanth Sripathi"
      "Email" : "asd@upp.com",
      "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
      }
    }
  }
}
about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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

about 4 years ago · Santiago Trujillo Denunciar

0

asd@upp.com"
about 4 years ago · Yurany andrea Valderrama tavera Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda