Meteor Blaze 显示阵列

Meteor Blaze display array

我有 collections 这样的:

例如,我想遍历 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>

但是不行,模板有什么问题?

theGame.questions 是具有 teema 键的对象数组的数组(您迭代)。因此,您仍然需要遍历第二级数组,或者在最终使用 teema 属性.

到达对象之前在该数组中定义特定项目

可能是这样的:

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

但这取决于你为什么首先要有这些 2 级数组。

{{teema}} 应该是什么?

无论如何,正如您从 console.log 语句中看到的那样,{{theGame.questions}} returns 另一个数组。但是那个数组 returns 对象。这真的很难用 Blaze 查询。

更好的解决方案是将其展平,以便您的数据形状如下:

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

这样您就不会在数组中嵌套数组。这将使您能够:

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

'#each theGame.questions' 在#with 内部不起作用,因为您可以直接访问 'theGame' 对象。

关键是当你试图在#with 中获取 Game 对象时,它会 return 你未定义,因为 'theGame' 对象没有你想要的游戏 属性,在 #with 块中访问。

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