ANSWER AT BOTTOM OF POST (ALSO SEE @soutot ANSWER)
I have successfully gained text output from my Firebase code and so I know it works. Now I need to loop all the children in Firebase. Coming from swift, the way to do it is to store each item on a tempObject then append that to an array. 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. The fully functional FB code I now have is:
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>. 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:
render() { let testHeader = "" let testText = "" ) } My Firebase database looks like this:
(I ignored subText for now)
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 }