admin管理员组文章数量:1414873
So, I am trying to retrieve data from my firestore and display it onto my webpage, I have tried everything and exhausted every question on this site to no avail.
When I use the code below, nothing renders on the website page, however when I use the mented code with dummy data instead of the firestore query retrieval data, the data renders as it should.
I have used console.log() on both the dummy data and the firestore data and they both log the same data array.
I am confused as to why the firestore data is not displaying the matches even though the array is saved correctly.
class MatchHistoryForm extends Component {
constructor(props) {
super(props);
var Matches = [];
firebase
.firestore()
.collection("Matches")
.orderBy("date")
.limit(10)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
Matches.push({
team1: doc.data().team1,
team2: doc.data().team2,
winner: doc.data().winner,
date: doc.data().date
});
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
// var Matches = [{
// team1: "asdf",
// team2: "jkl",
// winner: "team1",
// date: "1/2/2018",
// }, {
// team1: "qwer",
// team2: "yuio",
// winner: "team2",
// date: "1/8/2018",
// }];
console.log(Matches);
this.state = {
Matches: Matches
};
}
render() {
return (
<div id="against">
{this.state.Matches.map(v => {
return (
<p>
Team1: {v.team1}, Team2: {v.team2}, Winner: {v.winner}, Date:{v.date}
</p>
);
})}
</div>
);
}
}
So, I am trying to retrieve data from my firestore and display it onto my webpage, I have tried everything and exhausted every question on this site to no avail.
When I use the code below, nothing renders on the website page, however when I use the mented code with dummy data instead of the firestore query retrieval data, the data renders as it should.
I have used console.log() on both the dummy data and the firestore data and they both log the same data array.
I am confused as to why the firestore data is not displaying the matches even though the array is saved correctly.
class MatchHistoryForm extends Component {
constructor(props) {
super(props);
var Matches = [];
firebase
.firestore()
.collection("Matches")
.orderBy("date")
.limit(10)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
Matches.push({
team1: doc.data().team1,
team2: doc.data().team2,
winner: doc.data().winner,
date: doc.data().date
});
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
// var Matches = [{
// team1: "asdf",
// team2: "jkl",
// winner: "team1",
// date: "1/2/2018",
// }, {
// team1: "qwer",
// team2: "yuio",
// winner: "team2",
// date: "1/8/2018",
// }];
console.log(Matches);
this.state = {
Matches: Matches
};
}
render() {
return (
<div id="against">
{this.state.Matches.map(v => {
return (
<p>
Team1: {v.team1}, Team2: {v.team2}, Winner: {v.winner}, Date:{v.date}
</p>
);
})}
</div>
);
}
}
Share
Improve this question
edited Jul 11, 2018 at 1:20
Tholle
113k22 gold badges208 silver badges197 bronze badges
asked Jul 11, 2018 at 1:08
Evan MurrayEvan Murray
431 silver badge5 bronze badges
0
1 Answer
Reset to default 8The firebase request is asynchronous, so it will not be pleted before the constructor is run.
You could put that logic in ponentDidMount
instead and use setState
to update Matches
when it is done:
Example
class MatchHistoryForm extends Component {
state = { Matches: [] };
ponentDidMount() {
firebase
.firestore()
.collection("Matches")
.orderBy("date")
.limit(10)
.get()
.then(querySnapshot => {
const Matches = [];
querySnapshot.forEach(function(doc) {
Matches.push({
team1: doc.data().team1,
team2: doc.data().team2,
winner: doc.data().winner,
date: doc.data().date
});
});
this.setState({ Matches });
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
}
render() {
return (
<div id="against">
{this.state.Matches.map(v => {
return (
<p>
Team1: {v.team1},
Team2: {v.team2},
Winner: {v.winner},
Date: {v.date}
</p>
);
})}
</div>
);
}
}
本文标签: javascriptReact Firestore data retrieval into array not workingStack Overflow
版权声明:本文标题:javascript - React Firestore data retrieval into array not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745225639a2648594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论