如何 return 使用 meteor-boilerplate 创建的 Meteor 项目的代码集合?

How to return a collection in code from a Meteor project created with meteor-boilerplate?

这是我使用的代码:

Contacts = new Mongo.Collection('contacts');
Template.contact.helpers({
  contact: function() {
  return Contacts.find({});
  }
});

但是 HTML 没有返回集合。

在您的 HTML 文件中您需要定义模板:

<template name="Contacts"> 
{{#each contacts}}
    {{name}}
{{/each}}
</template>

在您的 java 脚本中,您将定义助手模板和 return 联系人集合。

Contacts = new Mongo.Collection('contacts');
    Template.Contacts.helpers({
        'contacts': function(){
            return Contact.find()
        }

});

查看本教程了解更多信息 - How To Create Templates in Meteor - Meteor Tutorial

如果你看the meteor-boilerplate website,你可以看到

"insecure" and "autopublish" are removed by default!

默认情况下,Meteor 包含 autopublish 包,它使数据库中的所有数据对客户端可用。这只适合早期开发,任何真正的项目都会去掉它。所以 meteor-boilerplate 默认删除它。

没有autopublish,您将需要自己发布数据。你可以试试这个:

// server code
Meteor.publish("contacts", function () {
    return Contacts.find();
});

// client code
Meteor.subscribe("contacts");

那么您现有的代码应该可以工作。

有关详细信息,请参阅 Meteor 文档中的 publish and subscribe