【问题标题】:Meteor Collection is not displaying流星集合未显示
【发布时间】:2016-03-05 08:34:37
【问题描述】:

我的流星收藏没有显示。我不明白为什么。我已经创建了我的收藏,然后从终端插入了一些记录 像 : db.tasks.insert({ text: "Hello world!", createdAt: new Date() });

这是我的代码:

HTML/

<head>
  <title>bdn</title>
</head>

<body>
  <h1>Welcome to Meteor!</h1>
  <ul>
      {{#each tasks}}
        {{text}}
      {{/each}}
    </ul>
</body>

<template name="Tasks">
{{text}}
</template>

MAIN.JS

Tasks = new Mongo.Collection("tasks");

if (Meteor.isClient) {
  // This code only runs on the client
  Template.Tasks.helpers({
    tasks: function () {
      return Tasks.find({});
    }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}

【问题讨论】:

    标签: mongodb meteor collections


    【解决方案1】:

    我建议避免在任何 html 文件中使用 &lt;body&gt;。 Meteor 会为您解决这个问题。我还建议为您的头部创建一个单独的 head.html 文件。

    html:

    <template name="body">
      <h1>Welcome to Meteor!</h1>
      <ul>
          {{#each tasks}}
            {{text}}
          {{/each}}
        </ul>
    </template>
    

    js:

    Tasks = new Mongo.Collection("tasks");
    
    if (Meteor.isClient) {
      Template.body.helpers({
        tasks: function () {
          return Tasks.find({});
        }
      });
    }
    

    【讨论】:

    • 使用 wrt.正文模板?如果我有多个部分都有自己的模板,你会得到一个复杂的嵌套正文模板。
    • 一旦开始路由(使用 flow 或 iron-router),您会希望模板具有更高的可重用性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 2016-02-27
    相关资源
    最近更新 更多