I'm trying to get some data from firebase and put it in an Array
, the value in question is listquestion
. For debugging I tried to show it using listquestion.map
but it's not showing anything but if I look at my console, the Array
has 3 objects inside it. I'm assuming the error is when I trying to put the data in the array but I did not find how to fix it
export function onceGetQuestion() {
var listquestion = [
{
answercount: "",
question: "",
status: ""
}
];
db.ref("questions")
.once("value")
.then(function(snaps) {
const foo = snaps.val();
if (foo !== null) {
Object.keys(foo).forEach(key => {
db.ref("questions")
.child(key)
.once("value")
.then(function(snap) {
var questionlist = listquestion;
questionlist.push({
answercount: snap.val().answercount,
question: snap.val().question,
status: snap.val().status
});
listquestion = questionlist;
});
});
}
});
console.log(listquestion);
return listquestion;
front end (react )
class HomePage extends Component {
constructor(props) {
super(props);
this.state = {
listquestion: [],
done: undefined
};
}
componentDidMount() {
this.state.listquestion = db.onceGetQuestion();
}
render() {
return (
questions.map(function(position, i){
........
<span >{position.question}</span>
........
}
.......
the array : [1]: https://i.stack.imgur.com/YL7zM.jpg