0
votes

I have collections like this:

enter image description here

I want to iterate over object.questions.teema for example.

I have helper:

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

and template:

<template name="game">

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

But it doesnt work, what is wrong with the template?

3

3 Answers

0
votes

What is {{teema}} supposed to be?

Regardless, as you can see from your console.log statement, {{theGame.questions}} returns another array. But that array returns objects. This is really hard to query for with Blaze.

The better solution would be to flatten it out so that your data is shaped like this:

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

This way you don't have an array nested in an array. That will allow you to:

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

'#each theGame.questions' Will not work inside the #with, because you can access the 'theGame' object directly.

The point is when you try to get theGame object inside the #with it will return you undefined, because 'theGame' object does not have theGame property, Which you want to access inside #with block.

<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
votes

theGame.questions is an array (that you iterate over) of array of objects which have the teema key. So you still need to iterate over the 2nd level array, or define a specific item in that array before you can eventually reach the object with teema property.

Maybe something like:

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

But it depends on why you have these 2-level arrays in the first place.