2014-03-26 77 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')。 –