2016-08-02 3 views
0

나는이 같은 컬렉션이 있습니다유성 블레이즈 디스플레이 배열

enter image description here

내가 예를 들어 object.questions.teema를 반복하고 싶습니다.

Template.game.helpers({ 
    theGame: function() { 
     var theGame = Game.findOne({_id:"LhQmaZKW8eJNenyX9"}) 
     console.log(theGame) 
     return theGame 
    } 
}); 

및 템플릿 :

<template name="game"> 

{{#with theGame}} 
    {{#each theGame.questions}} 
    {{teema}} 
    {{/each}} 
{{/with}} 
</template> 

그러나이 작업을 나던, 템플릿 잘못된 것입니다

나는 도우미가 있나요?

답변

0

.

요점은 'theGame'개체에 #with 블록 내부에서 액세스하려는 게임 속성이 없기 때문에 #with 내부에있는 Game 개체를 가져 오려고하면 정의되지 않은 개체가 반환된다는 것입니다.

<template name="game"> 
    {{#with theGame}} 
    {{#each questions}} 
     //Thie #each because you have nested array. As I can see in your console log. 
     {{#each this}} 
     {{teema}} 
     {{/each}} 
    {{/each}} 
    {{/with}} 
</template> 
0

theGame.questionsteema 키가있는 객체 배열의 배열입니다 (반복합니다). 따라서 여전히 2 수준 배열을 반복하거나 teema 속성을 사용하여 개체에 도달하기 전에 해당 배열에서 특정 항목을 정의해야합니다.

아마 뭔가 같은 :

{{#with theGame}} 
    {{#each questions}} 
    {{#each this}} 
     {{this.teema}} 
    {{/each}} 
    {{/each}} 
{{/with}} 

그러나 당신이 첫번째 장소에있는이 2 수준의 배열을 이유에 따라 달라집니다.

+0

그래서 이런 식으로 뭔가 : {{#with theGame}} {{#each의 theGame.questions}} {{#each의 questions.teema}} {{TEEMA}} {{/ 각}} {{/ each}} {{/ with}} 이 중 하나가 작동하지 않는 것 같습니다 – Villemh

+0

아니요. 시험용 코드로 수정 된 답변. 먼저 2 레벨 어레이가 필요한 이유를 확인하십시오. – ghybs

+0

좋은 제안 이었지만 2 레벨 어레이가 필요하지 않았습니다. 고맙습니다. – Villemh

0

{{teema}}은 무엇입니까?

console.log 문에서 알 수 있듯이 {{theGame.questions}}는 다른 배열을 반환합니다. 하지만 그 배열은 객체를 반환합니다. Blaze로 쿼리하기가 정말 어렵습니다.

더 나은 솔루션은 데이터가이 모양 않는 곳에 평평하게하는 것입니다 :

questions: [ 
    { 
     a: 'asdfkjah', 
     level: 'askdjfhal', 
     q: 'asdkfh', 
     teema: 'asdkfjh' 
     vaartus: 100 
    }, 
    { 
     ... 
    } 
] 

이 방법 당신은 배열 배열에 중첩가 없습니다. 즉, 당신에게 수 :

직접 'theGame'개체에 액세스 할 수 있기 때문에 'theGame.questions이 #each'는 #with 내부에서 작동하지 않습니다
{{#with theGame}} 
    {{#each theGame.questions}} 
    {{this.teema}} 
    {{/each}} 
{{/with}}