I want to display an array elements in my Vuejs template.
I am getting data from API, formatting them into an associative Array with a function and then trying to display in the template :
export default {
components: {
DefaultHeader,
DefaultFooter
},
data() {
return {
info: null,
};
},
mounted() {
this.refreshData();
},
methods: {
refreshData() {
infoService.getInfo(this.$route.params.id)
.then(response => {
this.info = FormatHelper.BInfoFormat(response.data);
})
.catch(e => {
console.log(e);
});
}
}
}
When i try to display the first element in the template with this code :
{{ this.info.name }}
I get the error :
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'name')
But when I display the full Array Object at the exact same place in code, I can see that it's defined :
{{ this.info }}
shows :
{ "name": "B3", "call": "about", "G": "H" }
Also, I get the right data when i try to console log in my mounted() component function :
console.log(this.info.name)
What is wrong in the way I am trying to access data getting me this error ?
this.infoinitiallynulland gets populated later? try{{ this.info?.name }}- without atual code, or at least enough to know whatthis.infois, there's many reasons this will occur{{ this.info?.name }}the display is right, i get the name without any error, what is it doing ? And yes, this.info is first defined at null.is first defined at nullandnullhas no properties, therefore the error - either initially defined it as{}rather thannull(though this may break other logic) or usev-ifto conditionally show.nameor use what I suggested - the actual reason would be easy to describe with some meaningful code in your question