简体   繁体   English

使用 react native 在实时数据库 firebase 中检索孩子的孩子

[英]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:如果要显示每个子节点的子节点(因此名称为NewTask1等的节点),则需要两个嵌套循环:

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.

相关问题 使用本机反应更新 firebase 实时数据库中的 2 个子项 - Update 2 children in firebase realtime database with react native Firebase 实时数据库订单按孩子与大多数孩子 - Firebase realtime database order by child with most children 如何从 Firebase 实时数据库中获取数据,这些数据是随机密钥的子项 - 使用本机反应? - How do I fetch data from firebase realtime database which are children to a random key - using react native? 将 Firebase 实时数据库与 React Native 应用程序集成 - Integrating Firebase Realtime Database with React Native App Firebase 实时数据库不检索指定的子项 - Firebase realtime database don't retrieve specified child 如何在Firebase实时数据库中过滤和检索嵌套子级? - How to filter and retrieve nested child in Firebase realtime database? 如何使用Firebase Realtime数据库和React Native上传图像/文件? - How to upload an image / file using Firebase Realtime database and React Native? 无法从 React Native 中的 Firebase 实时数据库读取数据 - Unable to read data from Firebase Realtime Database in React Native 如何在 React Native 中将 Firebase 实时数据库数据值加一? - How to increment Firebase Realtime database data values by one in React Native? 如何在反应原生中修改 firebase 实时数据库中的数组值 - How to modify array values in firebase realtime database in react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM