How to retrieve data from Firebase using Javascript?

后端 未结 2 2043
执笔经年
执笔经年 2021-02-10 09:16

I\'m trying to access \"id\" and store it in a variable. So far the code I have is:

var ref = firebase.database().ref(\"users\");

ref.on(\"value\", fun         


        
2条回答
  •  囚心锁ツ
    2021-02-10 09:56

    You need to get key of parent-node(L2L6vBsd45DFGfh) to access id child

    When you use push() method in firebase it will automatic generate unique key for your record. use set() or update() method to create your own custom keys.

    var ref = firebase.database().ref("users");
    ref.on("value", function(snapshot) {
        var childData = snapshot.val();
        var key = Object.keys(childData)[0];    //this will return 1st key.         
        console.log(childData[key].id);
    });
    

    You will use for-loop or forEach to get all keys.

提交回复
热议问题