0

I have a database structure like this enter image description here

firebase.auth().onAuthStateChanged((user) => {
    if (user) {
        database = firebase.database();
        var dbRef = firebase.database().ref().child('Agents').child(newarray[i]).orderByKey();

        dbRef.on('value', newAgents,  errData);
    }
})

function newAgents(data) { 
    var container = document.getElementById("team"); 
    container.innerHTML = '';

    data.forEach(function(AgentSnap) { // loop over all jobs

        console.log(AgentSnap.key);
        var Agents = AgentSnap.val();
        var AgentCard = `
            <div class= "profilepics" id="${key}">
                <figure ><img src=${Agents.profilepicurl}><figcaption>${Agents.Fname}</figcaption></figure>
            </div>
         `;

        container.innerHTML += AgentCard;
    })
}

but a console log of the firebase datasnapshot key shows this

enter image description here how can I change the key from the child nodes to for example 19777873?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ola
  • 431
  • 1
  • 8
  • 28
  • 1
    A likely culprit: I'd *strongly* recommend against using purely numeric keys for the Firebase Realtime Database. The SDK will automatically try to coerce it into an array. What if you prefix with an underscore, e.g. `_12354666` – Michael Bleigh Jul 24 '17 at 21:28
  • ok I will generate alphanumeric keys – Ola Jul 24 '17 at 21:38
  • @MichaelBleigh While I agree with your recommendation, I doubt it's the cause of the problems for this data set. The data is extremely sparse and the SDK is smart enough to detect that this is *not* an array. – Frank van Puffelen Jul 25 '17 at 03:37
  • @Ola: it looks like you're attaching your listener too low in the tree. Can you edit your question to include the code where you attach a listener and call `newAgents` with the snapshot? – Frank van Puffelen Jul 25 '17 at 03:38
  • @FrankvanPuffelen I have added the listener. The listener is actually located in another function. – Ola Jul 25 '17 at 11:09

1 Answers1

0

You attach your listener to a single agent:

var dbRef = firebase.database().ref().child('Agents').child(newarray[i]).orderByKey();

This means that the snapshot that is passed to your callback will contains the properties of a single agent. Then in your listener you loop over the children of the snapshot. This means you end up iterating over AuthId, Fname, Lname.

To get all agents, attach the listener to /Agents:

var dbRef = firebase.database().ref().child('Agents').orderByKey();
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thanks for the reply, the reason why I used newarray[i] is because I have sets of IDs that have been assigned to newarray and I was thinking that adding [I] will cause it to find each ID of newarray consecutively in the database. if I just reference .child('Agents'), it displays everything under agent which is too much for what I am looking for . I just want it to display only what matches what is in newarray – Ola Jul 25 '17 at 17:11
  • Firebase doesn't have a way to return a list of items by their key. See my answer here: https://stackoverflow.com/questions/29560088/firebase-equivalent-to-sql-where-in – Frank van Puffelen Jul 25 '17 at 19:39