2014-03-26 190 views
1

我已经创建了一个非常简单的示例来尝试让我的主题发布和订阅。流星发布/订阅

我删除:

自动发布 不安全

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

流星:PRIMARY> db.country.find(){ “_id”: 的ObjectId(” 5332b2eca5af677cc2b1290d“),”country“:”new zealand“, ”city“:”auckland“}

我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很陌生。我非常喜欢这个框架。

干杯

+0

如果您键入'Country.findOne()',您会在客户端控制台中获得什么?我想这可能只是因为浏览器无法显示'Template.test.country'帮助器返回的对象数组。 – user728291

+0

我得到国家没有定义,谢谢你的回复 –

回答

2

一切正常,因为它应该。如果你想打印出所有的文件,你必须使用每个帮助:

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

谢谢佩普LG奏效,我修改了我的.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中输入查看集合?在客户端上提供此数据我认为我仍然可以从控制台进行查询,因为我没有实现任何方法。

谢谢你的帮助。

干杯

+0

你的收藏“存储”在变量“国家”,这是你的js文件中的局部变量。如果你想在文件外部使用它,创建一个全局变量(从头开始删除'var')。 –