1
항목 목록이 있으며 각 항목에 Photo
항목이있을 수 있습니다.Handlebarsjs 개체 배열 : TypeError : 정의되지 않은 'id'속성을 읽을 수 없습니다.
this
을 사용하여 반복 중입니다.
모든 항목이있을 때 잘 수있는 Photo
:
<script id='library-template' type="text/x-handlebars-template">
{{#this}}
<div class='box'>
<a href='#insight' data-insight_id="{{Item.id}}">
{{#if Photo.0.id }}
{{Photo.0.photo}}
{{/if}}
</a>
<div class='subtitle'>
<h2>{{Item.title}}</h2>
<p>Added: {{Item.created}}</p>
</div>
</div>
{{/this}}
</script>
현재 객체는 사진 객체가없는 경우 : 내 개체의
TypeError: Cannot read property 'id' of undefined
형식 :
[
Object
Item: Object
Photo: Array[1]
Rating: Array[0]
Tag: Array[0]
__proto__: Object
,
Object
Item: Object
Photo: Array[0]
Rating: Array[0]
Tag: Array[0]
__proto__: Object
,
Object
,
Object
,
Object
]
을
두 번째 개체에는 Photo
레코드가 없습니다. 그래.
사진에 내용이 없는데도 오류가 발생하지 않도록 사진 객체에 액세스하려면 어떻게해야합니까?
참고 : {{#if Photo.0.id}} 대신 {{#if Photo.0.}}을 테스트하면 결과 :
오류 : 5 번 행의 구문 분석 오류 : ...} "> {{# # if Photo.0}} {{Photo.0 ----------------------^ID '가 필요함
고정 : 경우에 누군가 여기에 추가하면 같은 문제로 실행 : \t \t \t \t \t \t{{} Photo.length의 #if}\t \t \t {{Photo.0.photo}} \t \t \t \t {{if if}} – Wils