[英]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()
方法,因此您必须:
Object.keys()
. Object.keys()
提取密钥。 Array.forEach()
or Array.forEach()
. Array.forEach()
或Array.forEach()
键。 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.