开始使用 流星 - 显示集合

Getting started w/ Meteor - Display Collection

本文关键字:显示 集合 流星 开始      更新时间:2023-09-26

我是Javascript和Meteor的新手 - 我确信我犯了一个非常基本的错误。

我正在尝试了解如何显示集合的内容。

这是基于Meteor网站上的教程 - 我用"sw"替换了"任务",这也是我的集合的名称

 Template.HomePrivate.helpers({
});
sw = new Mongo.Collection("sw");
if (Meteor.isClient) {
  // This code only runs on the client
  Template.body.helpers({
    text: function () {
      return sw.find({});
    }
  });
}

.HTML:

<template name="HomePrivate">
    <div class="page-container container" id="content">
        <div class="row" id="title_row">
            <div class="col-md-12">
                <h2 id="page_title" class="pull-left">
                    Welcome {{userFullName}}!
                </h2>
                <div id="page_menu" class="pull-right">
                </div>
            </div>
        </div>
    </div>
  <div class="container">
    <header>
      <h1>Sight Words</h1>
    </header>
 <p>begin list</p>
    <ul>
      {{#each sw}}
        {{> sw}}
      {{/each}}
    </ul>
    <p>end list</p>
  </div>
</template>
<template name="sw">
  <li>{{text}}</li>
</template>

您的 #each 循环未显示任何内容,因为 sw 未定义为帮助程序:

{{#each sw}}
  {{> sw}}
{{/each}}

Blaze 在使用您的 #each 循环时会查找帮助程序名称 - 我认为这就是您的困惑所在。在这种情况下,您希望使用上面定义的帮助程序,text

{{#each text}}
  {{> sw}}
{{/each}}

sw还没有被定义为帮手,所以Blaze没有意识到这一点。

现在,{{> sw}}是有效的语法,因为它被定义为模板(这就是>符号的用途)。假设您的 sw 集合有一个字段text,您的 #each 循环现在应该正确显示。