为什么我的 select 没有填充我的 MongoDB Collection?

Why is my select not being populated with my MongoDB Collection?

在我的 Meteor 应用程序中,我正在发布:

ForeignVisaTypes = new Mongo.Collection("foreignVisaTypes");

if (Meteor.isServer) {

    Meteor.publish("foreignVisaTypes", function () {
      return ForeignVisaTypes.find();
    });
}

...并订阅:

if (Meteor.isClient) {

  Meteor.subscribe("foreignVisaTypes");
  . . .
}

...a Collection 其中 填充值。

但是,这种填充 select 的尝试失败了:

<select name="selvisatype" id="selvisatype" title="Please select a visa type">
    {{#each foreignVisaTypes}}
      <option value={{value}}>{{display}}</option>
    {{/each}}
</select>

为什么会失败?

订阅 collection 是不够的。它不能作为助手直接在空格键中使用。所以你还是需要定义一个helper来暴露它。

只需将此添加到您的客户端代码中,事情就会开始工作(将 myTemplate 替换为您的模板名称):

Template.myTemplate.helpers({
    foreignVisaTypes: function() {
        return ForeignVisaTypes.find();
    }
});