简体   繁体   English

React Native和Firebase数据库没有映射功能

[英]React native and Firebase database doesnt map function

I have been working for a long time in object parse, this object from Firebase but I can't parse, pls help me how can I do? 我在对象解析方面已经工作了很长时间,这个对象来自Firebase,但是我无法解析,请帮我怎么办?

{
   "user" : {
       "qweqwe1231" : {
           "-LKmfJ8X9FtL75-03yBf" : {
           "gonderilenkisi":"asdasdad",
           "gonderimzamani":"3123",
           "metin":"dqweqweqe",
           "okundumu":0
       },
       "-LKmgiBm8EB7LFDeOGbH" : {
           "gonderilenkisi":"asdasdad",
           "gonderimzamani":"3123",
           "metin":"dqweqweqe",
           "okundumu":0
       }
   }
}

I get data like this 我得到这样的数据

firebase.database().ref().child('user').child('qweqwe1231').on('value', 
     (data)=>{
         console.log(data.val())

});

And I use to map function but I get an error message "map is not a function" 而且我使用了map函数,但收到一条错误消息“ map不是函数”

firebase.database().ref().child('user').child('qweqwe1231').on('value', 
     (data)=>{
         data.val().map((item)=>{ console.log(item.gonderilenkisi)})

});

That error looks correct to me. 该错误在我看来是正确的。 If you load /user/qweqwe1231 you get this JSON: 如果加载/user/qweqwe1231则会得到以下JSON:

{
  "-LKmfJ8X9FtL75-03yBf" : {
    "gonderilenkisi" : "asdasdad",
    "gonderimzamani" : "3123",
    "metin" : "dqweqweqe",
    "okundumu" : 0
  }, 
  "-LKmgiBm8EB7LFDeOGbH" : {
    "gonderilenkisi" : "asdasdad",
    "gonderimzamani" : "3123",
    "metin" : "dqweqweqe",
    "okundumu" : 0
  }
}

This is not an array, so it doesn't have a map() method on it. 这不是数组,因此上面没有map()方法。

You're probably looking to iterate over the sub-objects of the object. 您可能正在寻找对象的子对象。 Since there is no map() method, you'll have to: 由于没有map()方法,因此您必须:

  1. extract the keys with Object.keys() . 使用Object.keys()提取密钥。
  2. loop over the keys with Array.forEach() or Array.forEach() . 使用Array.forEach()Array.forEach()键。
  3. look up the subobject for each key and do your thing on that. 查找每个键的子对象,然后在该对象上执行操作。

So in code: 因此在代码中:

firebase.database().ref().child('user').child('qweqwe1231').on('value', (snapshot)=>{
    let data = snapshot.val();
    let keys = Object.keys(data);
    keys.forEach((key) => { console.log(data[key]); });
});

Alternatively, you can loop over the children of the DataSnapshot that Firebase passes you with DataSnapshot.forEach() . 或者,你也可以遍历的子女DataSnapshot是火力地堡通过你DataSnapshot.forEach()

So in code: 因此在代码中:

firebase.database().ref().child('user').child('qweqwe1231')
.on('value',  (snapshot)=>{
    snapshot.forEach((item) => { console.log(item.val().gonderilenkisi); })
});    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM