简体   繁体   English

如何在React Native中循环所有Firebase子级?

[英]How do I loop all Firebase children in React Native?

ANSWER AT BOTTOM OF POST (ALSO SEE @soutot ANSWER) 答AT BOTTOM邮政 (另见@soutot ANSWER)

I have successfully gained text output from my Firebase code and so I know it works. 我已经从Firebase代码成功获取了文本输出,因此我知道它可以工作。 Now I need to loop all the children in Firebase . 现在,我需要循环所有Firebase的孩子。 Coming from swift, the way to do it is to store each item on a tempObject then append that to an array. 来自swift,实现此目的的方法是将每个项目存储在tempObject上,然后将其追加到数组中。 Then you can simply use that array any way you like. 然后,您可以随意使用该数组。 This doesn't seem to work with JavaScript , or I'm doing something wrong. 这似乎不适用于JavaScript ,或者我做错了事。 The fully functional FB code I now have is: 我现在拥有的功能齐全的FB代码是:

componentDidMount(){
    let child = "" 
    var ref = firebase.database().ref("testCategory");

    ref.once("value")
    .then(function(snapshot) {
        child = snapshot.child("test1/testHeader").val() 
        this.setState( {    
            child
        })
    }.bind(this)); 
}

I can then successfully print this in console or in <Text> . 然后,我可以在控制台或<Text>成功打印此内容。 Now, the problem I'm having is looping all children, adding them to an array, then using that array to display the data. 现在,我遇到的问题是循环所有子代,将它们添加到数组中,然后使用该数组显示数据。 In my head, this is how it should work: 在我看来,这应该是这样的:

componentDidMount(){
    let child = "" 
    var ref = firebase.database().ref("testCategory");

    ref.once("value")
    .then(function(snapshot) {
        snapshot.forEach(function(childSnapshot){
           let tempObject = MyFirebase
           tempObject.testHeader = 
           childSnapshot.val() 

           myArray.append(tempObject) //or .push???
        })
        this.setState( {    //PASSING VARIABLE TO STATE
            child   
        })
    }.bind(this)); //BINDING TO SET STATE
}

I know that this is obviously wrong. 我知道这显然是错误的。 Creating an object like that doesn't even work... MyFirebase -class looks like this: 创建这样的对象甚至行不通... MyFirebase -class看起来像这样:

render() {
let testHeader = ""
let testText = ""   
)
}

My Firebase database looks like this: 我的Firebase数据库如下所示: FB数据库 (I ignored subText for now) (我暂时忽略了subText)

All suggestions are much appreciated :) 所有建议都非常感激:)

WORING CODE 工作代码

 //FIREBASE CODE 
componentDidMount(){
    const ref = firebase.database().ref("testCategory");

    ref.once("value")
    .then(function(snapshot) {
        const categories = []

        //LOOPING EACH CHILD AND PUSHING TO ARRAY
        snapshot.forEach(item => {

            const temp = item.val();
            categories.push(temp);
            return false;
        });

        this.setState( {    //PASSING VARIABLE TO STATE
            child,
            categories
        })
    }.bind(this)); //BINDING TO SET STATE
}

According to the code you provided, it looks like it works until this point 根据您提供的代码,到目前为止,它看起来仍然有效

var ref = firebase.database().ref("testCategory");

    ref.once("value")
    .then(function(snapshot) {

Am I correct? 我对么?

From this point, if you add a console.log(snapshot.val()) it might print an array of objects, something like this: [ { test1: { testHeader: 'FirstHeader', testText: 'FirstText' } }, { test2: { testHeader: 'SecondSub', testText: 'SecondSub } }] 从这一点来看,如果添加console.log(snapshot.val())它可能会打印对象数组,如下所示: [ { test1: { testHeader: 'FirstHeader', testText: 'FirstText' } }, { test2: { testHeader: 'SecondSub', testText: 'SecondSub } }]

Right? 对?

If so, you can for example store this snapshot into your state and then consume this state in your render method. 如果是这样,您可以例如将该快照存储到您的状态中,然后在渲染方法中使用此状态。 Something like this: 像这样:

const ref = firebase.database().ref('testCategory');

ref.once('value').then((snapshot) => {
  this.setState({ categories: snapshot.val() });
});

Then in your render method: 然后在您的渲染方法中:

const { categories } = this.state;

categories.map(category => <Text>{category.testHeader}</Text>)

The result in your screen should be: FirstHeader SecondSub 屏幕中的结果应为:FirstHeader SecondSub

Let me know if this helped 让我知道这是否有帮助

Some links that might explain more about es6 codes I used in this example: 一些链接可能会解释我在本示例中使用的es6代码的更多信息:

array map categories.map(...) : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map 数组贴图categories.map(...)https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map

object destructuring const { categories } = this.state : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment 对象解构const { categories } = this.statehttps//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

const instead of var const ref = ... : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const const而不是var const ref = ...https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/const

setState: https://reactjs.org/docs/state-and-lifecycle.html setState: https : //reactjs.org/docs/state-and-lifecycle.html

arrow function (snapshot) => ... : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions 箭头功能(snapshot) => ...https//developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Some about firebase snapshots: https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot 有关Firebase快照的一些信息: https : //firebase.google.com/docs/reference/js/firebase.database.DataSnapshot

Hope it helps 希望能帮助到你

暂无
暂无

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

相关问题 如何从 Firebase 实时数据库中获取数据,这些数据是随机密钥的子项 - 使用本机反应? - How do I fetch data from firebase realtime database which are children to a random key - using react native? 如何访问 React Native Web 中引用的子组件? - How do I access children components of a reference in React Native Web? 如何在 React Native 中显示 Firebase 存储中的所有图像而不需要图像名称? - How do I display all of the images from my Firebase Storage in React Native without needing image names? 如何从集合中检索所有文档及其相关信息? (反应原生 Firebase) - How do i retrieve all documents from a collection and its relevant information? (React Native Firebase) 我如何使这个循环所有孩子递归? - How do I make this loop all children recursively? 清除钩子上的异步调用 -React Native/Firebase 我该怎么做? - Clear asynchronous calls on hooks -React Native/Firebase how I do? 如何使用Javascript获取所有孩子(即孩子的孩子)? - How do i get all children (ie children of children) with Javascript? 使用本机反应更新 firebase 实时数据库中的 2 个子项 - Update 2 children in firebase realtime database with react native Firebase的云功能:如何为所有子项的特定子键触发数据库功能 - Cloud Functions for Firebase: How do I trigger a database function for a specific child key of all children 如何将Firebase中特定ref的所有子级放入对象? - How do you get all the children of specific ref in firebase into an object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM