I am trying to print out the name property of an object using Vue.
Here's what I tried {{ selectedTopic.name }}, this throws this error Uncaught TypeError: Cannot read property 'name' of null
If I print out {{ selectedTopic }} it will wait until that data is available and then display without errors.
Now I understand that the data I'm requesting is not available at that stage, but why is it throwing an error? Shouldn't it just wait until that data becomes available like it does for {{ selectedTopic }}?
Vue app
var qaSuggest = new Vue({
el: '#qa-suggest',
data: {
selectedTopic: null,
profiles: []
},
mounted() {
bus.$on("send-topic", selectedTopic => this.selectedTopic = selectedTopic);
},
updated(){
axios.get('/vueprofiles').then(response => this.profiles = response.data);
console.log('selectedTopic: ', this.selectedTopic.name);
}
});
HTML
<div id="qa-suggest">
<ul class="list-unstyled">
<li v-for="profile in profiles">
<div class="clearfix" style="border-bottom: 1px solid #ddd; padding: 5px;">
<div class="pull-left">
<img :src="profile.picture" class="img-responsive" style="max-width: 60px; margin-right: 10px;">
</div>
<div class="pull-left">
<p style="font-size: 12px; margin-bottom: 0;">{{ profile.category }} / {{ profile.subcategory }}</p>
<p><strong>{{ profile.name }}</strong></p>
</div>
</div>
</li>
</ul>
Topic: {{ selectedTopic.name }}