我是MeteorJS的新手。我尝试使用以下代码在客户端视图中显示MongoDB集合。
客户端/主.js
Resolutions = new Mongo.Collection('resolutions');
Template.body.helpers({
resolutions : function(){
return Resolutions.find();
}
});
client/main.html(这里使用火焰(
<head>
<title>resolutions</title>
</head>
<body>
<ul>
{{#each resolutions}}
{{>resolution}}
{{/each}}
</ul>
</body>
<template name="resolution">
<li>{{title}}</li>
</template>
然后我使用流星蒙戈贝壳将一些对象插入到集合中
db.resolutions.insert({title:"test", createdAt:new Date()});
并且 I 测试天气将对象插入到集合中使用
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');
/客户端/主.js
Template.body.helpers({
resolutions : function(){
return Resolutions.find();
}
});
Resolutions.find();
返回游标而不是数组。请改用fetch()
方法:
Template.resolutions.helpers({
resolutions: function(){
return Resolutions.find().fetch();
}
});
client/main.html
<head>
<title>resolutions</title>
</head>
<body>
<template name="resolution">
<ul>
{{#each resolutions}}
<li>{{title}}</li>
{{/each}}
</ul>
</template>
</body>