2017-02-09 12 views
1

MeteorJSの新機能です。クライアントビューでMongoDBコレクションを表示するために以下のコードを試しました。Mongo DBコレクションをMeteorJSのクライアントメインビューに表示するには?

クライアント/ main.js

Resolutions = new Mongo.Collection('resolutions'); 

Template.body.helpers({ 
    resolutions : function(){ 
     return Resolutions.find(); 
    } 
}); 

クライアント/ main.htmlを(炎がここで使用される)

<head> 
    <title>resolutions</title> 
</head> 

<body> 
    <ul> 
     {{#each resolutions}} 
     {{>resolution}} 
     {{/each}} 
    </ul> 
</body> 

<template name="resolution"> 
    <li>{{title}}</li> 
</template> 

そして私は、流星のmongoを使用してコレクションにいくつかのオブジェクトを挿入シェル

db.resolutions.insert({title:"test", createdAt:new Date()}); 

私のテスト天候オブジェクトは

db.resolutions.find() 

を使用して、コレクションに挿入し、出力された、

{ 
    "_id": ObjectId("589c8d1639645e128780c3b4"), 
    "title": "test", 
    "createdAt": ISODate("2017-02-09T15:39:02.216Z") 
} 

しかし、予想通りのクライアントビューでオブジェクトのタイトルがリストに表示されませんされています。代わりに、空の画面が表示されます。

答えて

0

あなたはほとんどそこにいるようですが、あなたのコレクションを公開して購読するための適切な宣言が欠落しているようです。

あなたは、これは親切で公式流星のチュートリアルで文書を見つけることができます:あなたはまだあなたがクライアントとサーバーの両方で、あなたのコレクションを宣言する必要がautopublishを使用していると仮定すると、https://www.meteor.com/tutorials/blaze/publish-and-subscribe

0

。これを行う最も簡単な方法は、/libで宣言することです。

/lib/collections.jsカーソルとしない配列を返すResolutions.find();

Resolutions = new Mongo.Collection('resolutions'); 

/client/main.js

Template.body.helpers({ 
    resolutions : function(){ 
     return Resolutions.find(); 
    } 
}); 
0

Template.resolutions.helpers({ 
    resolutions: function(){ 
     return Resolutions.find().fetch(); 
    } 
}); 

クライアント/ main.htmlを

<head> 
    <title>resolutions</title> 
</head> 

<body> 
    <template name="resolution">  
     <ul> 
      {{#each resolutions}} 
      <li>{{title}}</li> 
      {{/each}} 
     </ul> 
    </template> 
</body> 
:代わりに fetch()メソッドを使用します
関連する問題