流星发布/订阅

时间:2014-03-26 11:11:17

标签: meteor

我已经创建了一个非常简单的示例来尝试让我的头部发布和订阅。

我删除了:

自动发布 不安全的

我的mongo数据库看起来像这样

  

流星:PRIMARY> db.country.find(){“_ id”:   ObjectId(“5332b2eca5af677cc2b1290d”),“country”:“新西兰”,   “城市”:“奥克兰”}

我的test.js文件看起来像这样

var Country = new Meteor.Collection("country");

if(Meteor.isClient) {
    Meteor.subscribe("country");
    Template.test.country = function () {
        return Country.find();
      };
  }

if(Meteor.isServer) {
    Meteor.publish("country", function() {
        return Country.find();
    });
}

我的html文件看起来像这样

<head>
    <title>test</title>
</head>

<body>
    {{> test}}
</body>

<template name="test">
    <p>{{country}}</p>
</template>

我不明白为什么这不起作用。我在服务器上发布,订阅它。我知道这不会是我在现场环境中会做的事情,但我甚至无法复制检索整个集合以在客户端上查看。

如果我改变了这个返回Country.find();返回Country.find()。count();我得到1.但是国家文本没有出现。

很想知道发生了什么。我是开发和使用Meteor的新手。我非常喜欢这个框架。

干杯

2 个答案:

答案 0 :(得分:2)

一切都按预期运作。如果要打印出所有文档,必须使用每个帮助程序:

<template name="test">
    {{#each country}}
        <p>{{country}}, {{city}}</p>
    {{/each}}
</template>

答案 1 :(得分:0)

谢谢Peppe L-G的工作,我稍微修改了我的.js文件是最终的结果:

.js文件

var Country = new Meteor.Collection("country");

if(Meteor.isClient) {

    Meteor.subscribe("country");
    Template.test.countries = function () {
    return Country.find();
  };
}

if(Meteor.isServer) {
    Meteor.publish("country", function() {
        return Country.find();
    });
}

html文件

<head>
  <title>test</title>
</head>

<body>
  {{> test}}
</body>

<template name="test">
    {{#each countries}}
        <p>{{country}}, {{city}}</p>
    {{/each}}
</template>

由于我的代码几乎是正确的,为什么我无法使用Country.findOne()查询控制台或通过键入Country来查看集合?在客户端上提供这些数据我认为我仍然可以从控制台查询,因为我没有实现任何方法。

感谢您的帮助。

干杯

相关问题