[英]How to display Mongo DB collection in client main view in MeteorJS?
我是MeteorJS的新手。 我尝试使用以下代码在客户端视图中显示MongoDB集合。
客户机/ main.js
Resolutions = new Mongo.Collection('resolutions');
Template.body.helpers({
resolutions : function(){
return Resolutions.find();
}
});
client / main.html (这里使用了blaze)
<head>
<title>resolutions</title>
</head>
<body>
<ul>
{{#each resolutions}}
{{>resolution}}
{{/each}}
</ul>
</body>
<template name="resolution">
<li>{{title}}</li>
</template>
然后我使用meteor mongo shell将一些对象插入到集合中
db.resolutions.insert({title:"test", createdAt:new Date()});
并且我测试天气将对象插入到集合中使用
db.resolutions.find()
输出是,
{
"_id": ObjectId("589c8d1639645e128780c3b4"),
"title": "test",
"createdAt": ISODate("2017-02-09T15:39:02.216Z")
}
但在客户端视图中,对象标题不会按预期显示在列表中。 而是查看空屏幕。
看起来你几乎就在那里,但似乎缺少适当的声明来发布和订阅你的收藏。
您可以在以下官方Meteor教程中找到有用的文档: https : //www.meteor.com/tutorials/blaze/publish-and-subscribe
假设您仍在使用autopublish
,则需要在客户端和服务器上声明您的集合。 最简单的方法是在/lib
声明它。
/lib/collections.js
Resolutions = new Mongo.Collection('resolutions');
/client/main.js
Template.body.helpers({
resolutions : function(){
return Resolutions.find();
}
});
Resolutions.find();
返回游标而不是数组。 改为使用fetch()
方法:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.