I'm pretty new in coding. I'm working on ReactJS and I am trying to display in a list all differents categories in my database from Firebase. You can see below the code I wrote:
import Link from 'next/link'
import React, { Component } from 'react'
import firebase from 'firebase'
export default class extends Page {
constructor () {
super()
this.state = {
datas: ''
}
}
componentDidMount () {
firebase.initializeApp(clientCredentials)
// ------- Connect to the firebase DATABASE -------- ////
firebase.database().ref().orderByChild("sectionIndex")
.on("child_added",
snapshot => {this.setState({datas: snapshot.val().category});},
error => {console.log("Error: " + error.code);});
}
render () {
return (
<ul>
{this.state.datas.map((data, i) =>
<li key={i}> {data.category} </li>
</ul>
)
}
}
It says that TypeError: this.state.datas.map is not a function
.
I believe that the data from firebase is not an array maybe thats why I have this message error...
Here is how my database looks like in Firebase: DB
Maybe the database is not an Array [] but Object {} that's why? If yes, how can I convert it into an Array.