[英]Retrieve children of child in realtime database firebase with react native
Im trying to retreive some data from realtime database firebase and display it on the screen.我试图从实时数据库 firebase 中检索一些数据并将其显示在屏幕上。 I have implemented a function snapshotToArray to convert Json to array.我已经实现了一个函数 snapshotToArray 将 Json 转换为数组。 I have the feedback collection and I want to retreive all the children of JAHC9F.. to display the contents of if child (inside new ,task1,task4 and new)我有反馈集合,我想检索 JAHC9F 的所有孩子。
Here in my case, I can see only two objets for two children but I cant see the contents of each feedback在我的例子中,我只能看到两个孩子的两个对象,但我看不到每个反馈的内容
*user.uid is equal to J4HC9FgMM.. in this case *user.uid 等于 J4HC9FgMM.. 在这种情况下
componentDidMount=async()=>{
const feedbacks=await firebase.database().ref("Feedback").child(user.uid).once('value');
const feedbackArray=snapshotToArray(feedbacks);
this.props.loadFeedbacks(feedbackArray);
}
renderItemFeedback = (item, index) => {
return (
<View>
<Text>{item.name}</Text>
<View>
<NetworkImage
source={{ uri: item.ImageFeedback }}
/>
</View>
<View>
<Text>{item.countLike}</Text>
</View>
</View>
);
};
render() {
return (
<CustomBackground>
<ScrollView>
<View>
<FlatList
data={this.props.feedbacks.feedbacks}
renderItem={({ item }, index) =>
this.renderItemFeedback(item, index)
}
keyExtractor={(item, index) => index.toString()}
/>
</View>
</ScrollView>
</CustomBackground>
);
}
}
Here the implementation of snapshotToArray function :这里是 snapshotToArray 函数的实现:
export const snapshotToArray= snapshot=>{
let returnArr=[]
snapshot.forEach(childSnapshot => {
let item=childSnapshot.val()
item.key=childSnapshot.key
returnArr.push(item)
});
return returnArr;
}
If you see two items in the array, that is working as expected as there are two child nodes under J4HC9FgMM..
in the screenshot.如果您在数组中看到两个项目,则说明正常工作,因为在屏幕截图中J4HC9FgMM..
下有两个子节点。
If you want to show the child nodes of each child node (so the nodes with names New
, Task1
, etc), then you need two nested loops:如果要显示每个子节点的子节点(因此名称为New
、 Task1
等的节点),则需要两个嵌套循环:
export const snapshotToArray= snapshot=>{
let returnArr=[]
snapshot.forEach(childSnapshot => {
childSnapshot.forEach(grandchildSnapshot => {
let item=grandchildSnapshot.val()
item.key=grandchildSnapshot.key
returnArr.push(item)
})
});
return returnArr;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.