How to retrieve data from Firebase using Javascript?

后端 未结 2 2045
执笔经年
执笔经年 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 10:17

    Do this:

     var ref = firebase.database().ref("users");
    
    ref.on("value", function(snapshot) {
     snapshot.forEach(function(childSnapshot) {
      var childData = childSnapshot.val();
      var id=childData.id;
      console.log(childData);
     });
    });
    

    In the above the location is at users then you use on() method to read data, and using the forEach loop, you iterate inside the pushid and get the values.

    To access each one alone you do this var ids=childData.id; or/and var names=childData.name;

    Also it is better to use once() as it only reads data once.

提交回复
热议问题