I am trying to access an Object property in vue component computed property but I am getting an error saying the property is undefined. I have setup a prototype here. When I try to use
playerEntry.stats.RecYards["#text"] > "0"
Vue complains
[Vue warn]: Error in render: "TypeError: playerentry.stats.RecYards is undefined"
and looking in vue.js devtools under Computed I see playerReceivingStats:"(error during evaluation)" here is the pertinent code:
boxscore.js
const boxScoresStats = {
stats: Vue.component("box-scores", {
props: ["props_box_score", "props_gameID"],
data: function() {
return {
playerStats: this.props_box_score.data.gameboxscore.awayTeam.awayPlayers
.playerEntry
};
},
computed: {
playerPassingStats: function() {
return this.playerStats.filter(playerEntry => {
return playerEntry.player.Position === "QB";
});
},
playerReceivingStats: function() {
return this.playerStats.filter(playerEntry => {
console.log(playerEntry.stats.RecYards["#text"]);
return playerEntry.stats.RecYards["#text"] > "0";
});
}
},
Template
<div v-for="playerStats in playerReceivingStats">
<tr class="d-flex">
<td class="col-3 justify-content-center" scope="row">
{{playerStats.player.FirstName}} {{playerStats.player.LastName}} ({{playerStats.player.Position}})
</td>
<td class="col-2 justify-content-center" justify-content="center">
{{ playerStats.stats.Receptions['#text'] }} </td>
<td class="col-3 justify-content-center">{{playerStats.stats.RecYards['#text']}}</td>
<td class="col-2 justify-content-center">{{playerStats.stats.RecTD['#text']}}</td>
<td class="col-2 justify-content-center">{{playerStats.stats.Targets['#text']}}</td>
</tr>
</div>
I have tried both bracket and dot notation but still no good. I see the values from the console.log printing out ok. But I am at a loss as to why it says undefined when I can see the object property in devtools. Also note if I shorten the eval in computed to playerEntry.stats.RecYards I get no error? But I then get incorrect results. Any help much appreciated.
Updated:
Here is the computed property that works:
playerReceivingStats: function() {
return this.offensivePlayers.filter(playerEntry => {
if (typeof playerEntry.stats.RecYards != "undefined") {
return playerEntry.stats.RecYards["#text"] > "0";
}
});
}
}