firebase快照键显示错误的键

时间:2017-07-24 21:03:09

标签: html firebase firebase-realtime-database

我有这样的数据库结构 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;
    })
}

但是firebase datasnapshot键的控制台日志显示了此

enter image description here 如何将密钥从子节点更改为例如19777873?

1 个答案:

答案 0 :(得分:0)

您将监听器附加到单个代理:

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

这意味着传递给回调的快照将包含单个代理的属性。然后在你的监听器中循环遍历快照的子节点。这意味着您最终会迭代AuthIdFnameLname

要获取所有座席,请将侦听器附加到/Agents

var dbRef = firebase.database().ref().child('Agents').orderByKey();