我是MeteorJS的新手。我尝试使用以下代码在客户端视图中显示MongoDB集合。
client/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>然后我使用将一些对象插入到集合中。
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")
}但是在客户端视图中,对象标题并没有按预期显示在列表中。取而代之的是一个空屏幕。
发布于 2017-02-09 16:28:07
看起来您已经接近完成了,但似乎缺少发布和订阅您的集合的适当声明。
您可以在以下网站的官方Meteor教程中找到这个有用的文档:https://www.meteor.com/tutorials/blaze/publish-and-subscribe
发布于 2017-02-09 18:28:06
假设您仍然在使用autopublish,您需要在客户机和服务器上声明您的集合。最简单的方法是在/lib中声明它。
/lib/collections.js
Resolutions = new Mongo.Collection('resolutions');/client/main.js
Template.body.helpers({
resolutions : function(){
return Resolutions.find();
}
});发布于 2017-02-10 08:50:01
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>https://stackoverflow.com/questions/42140983
复制相似问题