admin管理员组文章数量:1392099
console.log(this.sights)
returns an array with 20 objects that have a couple of properties like name
, photos
, references
, etc. Currently I am trying to loop through these objects and display their name
s and their photo_reference
s. I do this like this:
<div @click='selectSight(index)' v-for='(sight, index) in sights'>
{{ sight.name }}
{{ sight.photos[0].photo_reference }}
</div>
This displays the sight.name
. However, I get an error
"Error in render: "TypeError: Cannot read property '0' of undefined"".
I tried manually accessing like this:
console.log(this.sights[0].photos[0].photo_reference)
and it returns the photo_reference
. So, I'm not mistaking the properties. So what am I doing wrong?
This is the array structure:
(20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, __ob__: Observer]
0:
geometry: Object
icon: ".png"
id: "975708e853f136200f2229c793df9070707b842e"
name: "National Museum of Modern Art, Tokyo"
opening_hours: Object
photos: Array(1)
0:
height: 3456
html_attributions: Array(1)
photo_reference: "CmRZAAAA60seVRHbzyg3TPIUPEUaDbzI2-TpMB3cB3bC8BYG_gRrJwmwSVY1Mcl1PBo0U0CMwmqssyQw4w2iHyh6ze3iQaiXdsveolGBovi3rGZTgvKjTV9PRt-WDieYrwoRy1z0EhBsZitey_MyjiwrYK_Sol3eGhSfOpXUpLc-3RYeJjz2JKMMXYNZYw"
width: 4608
console.log(this.sights)
returns an array with 20 objects that have a couple of properties like name
, photos
, references
, etc. Currently I am trying to loop through these objects and display their name
s and their photo_reference
s. I do this like this:
<div @click='selectSight(index)' v-for='(sight, index) in sights'>
{{ sight.name }}
{{ sight.photos[0].photo_reference }}
</div>
This displays the sight.name
. However, I get an error
"Error in render: "TypeError: Cannot read property '0' of undefined"".
I tried manually accessing like this:
console.log(this.sights[0].photos[0].photo_reference)
and it returns the photo_reference
. So, I'm not mistaking the properties. So what am I doing wrong?
This is the array structure:
(20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, __ob__: Observer]
0:
geometry: Object
icon: "https://maps.gstatic./mapfiles/place_api/icons/museum-71.png"
id: "975708e853f136200f2229c793df9070707b842e"
name: "National Museum of Modern Art, Tokyo"
opening_hours: Object
photos: Array(1)
0:
height: 3456
html_attributions: Array(1)
photo_reference: "CmRZAAAA60seVRHbzyg3TPIUPEUaDbzI2-TpMB3cB3bC8BYG_gRrJwmwSVY1Mcl1PBo0U0CMwmqssyQw4w2iHyh6ze3iQaiXdsveolGBovi3rGZTgvKjTV9PRt-WDieYrwoRy1z0EhBsZitey_MyjiwrYK_Sol3eGhSfOpXUpLc-3RYeJjz2JKMMXYNZYw"
width: 4608
Share
Improve this question
edited Jan 25, 2019 at 4:10
adiga
35.3k9 gold badges65 silver badges87 bronze badges
asked Jan 24, 2019 at 20:00
OnyxOnyx
5,78210 gold badges52 silver badges119 bronze badges
6
- Where does the array e from? – Pointy Commented Jan 24, 2019 at 20:03
-
2
It's probably because you only tested
sights[0]
- the property must be missing from another member of the array. – Jeff Commented Jan 24, 2019 at 20:04 -
There must be some
sight
which hasphotos
set toundefined
. Check the entiresights
array – adiga Commented Jan 24, 2019 at 20:04 -
Don't forget; opening the object like that in the console only gets you the value at the time you clicked the down arrow. Most of the time, this is a timing thing. I don't know vue, but you should be able to echo
JSON.stringify(sights)
before that line in the HTML. – Heretic Monkey Commented Jan 24, 2019 at 20:04 - The array es from Google Maps API. I'm gonna check if there's an object that has empty photos. – Onyx Commented Jan 24, 2019 at 20:07
1 Answer
Reset to default 4You're getting that error because some of the objects have sight.photos
as undefined
. You could add a check like this before accessing the zeroth index:
<div @click='selectSight(index)' v-for='(sight, index) in sights'>
{{ sight.name }}
{{ sight.photos && sight.photos.length > 0 ? sight.photos[0].photo_reference : '' }}
</div>
本文标签: javascriptTypeError Cannot read property 39039 of undefined inside vforStack Overflow
版权声明:本文标题:javascript - TypeError: Cannot read property '0' of undefined inside v-for - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744780101a2624656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论