Access childs in firebase database

问题: I am using firebase realtime database but i cannot access the date,description and other fields when i use this code below: let dataRef = firebase.database().ref().chi...

问题:

Firebase Database View

I am using firebase realtime database but i cannot access the date,description and other fields when i use this code below:

let dataRef = firebase.database().ref().child('cars');
dataRef.on('value', function(data) {
console.log(data.val());
});

It returns all the childs in the cars node as a single object

Output: Object { "-LGixH1Hbjb01pVGa9AM": {…}, "-LGixXbCcbv082aohG3E": {…}, "-LGixnOd80UjF6PlSNhL": {…} }

Is there a way to access name value pairs inside the Keys(that are -LGixH1Hbjb01pVGa9AM,-LGixXbCcbv082aohG3E,-LGixnOd80UjF6PlSNhL)

Thanks in advance.


回答1:

Try the following:

let dataRef = firebase.database().ref().child('cars');
 dataRef.on('value', function(data) {
 data.forEach(function(childSnapshot) {
    let date=childSnapshot.val().date;
    let description=childSnapshot.val().description;
  });
});

The snapshot is at cars, you then loop inside the ids using forEach and retrieve the data that you want.

  • 发表于 2018-07-09 14:07
  • 阅读 ( 263 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除