I'm trying to reach the content of matchData
from a Vue method. I'm able to console.log(this.matchData)
, but not able to get its content.
When I console.log(this.matchData[0].matchScores[0])
under method readMatchPlayerScoreIds()
I get:
vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'matchScores' of undefined"
export default {
data() {
return {
matchData: [],
};
},
methods: {
readMatches() {
db.collection("matches")
.get()
.then((queryMatchSnapshot) => {
queryMatchSnapshot.forEach((doc) => {
this.matchData = [];
this.matchData.push({
awayscore: doc.data().awayscore,
homeScore: doc.data().homeScore,
matchScores: doc.data().matchscores,
})
});
console.log(this.matchData[0].matchScores[0])
});
},
readMatchPlayerScoreIds() {
console.log(this.matchData[0].matchScores[0])
}
},
mounted() {
this.readMatches();
this.readMatchPlayerScoreIds();
},
};
readMatchPlayerScoreIds
before the matchData is loaded. I suppose the db call is async, isn't it? A quick fix would be to do callreadMatchPlayerScoreIds
inside thethen
block after the data has been set. – macghriogair