Answer the question
In order to leave comments, you need to log in
How to get around the "Cannot read property '0' of null" error when accessing multiple empty object properties?
I receive data from the server in the form of an array of objects and through v-for I iterate through the array in
<li v-for="item of data.items" :key="item.id">
<a class="catalog__pic" href="#">
<img
:src="item.colors[0].gallery[0].file.url"
:alt="item.title"
/>
</a>
<h3 class="catalog__title">
<a href="#">
{{ item.title }}
</a>
</h3>
</li>
item.colors[0].gallery[0].file.url
methods: {
check(item) {
if(item.colors && item.colors[0] && item.colors[0].gallery && item.colors[0].gallery[0] ){
return item.colors[0].gallery[0].file.url
}
return '';
}
}
<template>
<li v-for="item of data.items" :key="item.id">
<a class="catalog__pic" href="#">
<img
:src="check(item)"
:alt="item.title"
/>
</a>
<h3 class="catalog__title">
<a href="#">
{{ item.title }}
</a>
</h3>
</li>
<template>
Answer the question
In order to leave comments, you need to log in
https://developer.mozilla.org/en/docs/Web/JavaScri...
https://developer.mozilla.org/en/docs/Web/JavaScri...
But this is from the 2020 standard, to support more older browsers will need babel + preset-env
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question