【发布时间】:2017-05-02 16:07:51
【问题描述】:
在我实现发布/订阅之前,我的代码运行良好。我关注了the basic tutorial 并检查了the source code,但我没有做任何不同的事情。一切都构建并运行,但 MongoDB 中的任何内容都不会显示在 Blaze 模板中。
imports/api/features.js
if (Meteor.isServer) {
Meteor.publish('features', function featuresPublication() {
return Features.find({});
});
Meteor.publish('comments', function commentsPublication() {
return Features.find({}, {fields: {comments: 0}});
})
};
客户端/main.js
Template.body.onCreated(function bodyOnCreated() {
Meteor.subscribe('features');
});
客户端/main.html
<body>
<h1 id="title">Feature Requests</h1>
{{#if currentUser}}
<button class="ui green button create" id="create">Add a New Feature Request</button>
{{> requestForm}}
{{#each features}}
{{> feature}}
{{/each}}
{{else}}
{{> loginButtons}}
{{/if}}
</body>
编辑#1
在我运行 meteor remove autopublish 之前,我的代码看起来像这样并且工作正常:
Template.body.helpers({
features() {
return Features.find({}, {sort: {createdAt: -1}});
},
comments() {
return Features.find({}, {fields: {comments: 0}});
},
});
编辑#2
感谢所有提供答案的人。我从根本上误解了发布/订阅是如何工作的。我没有意识到订阅后我还需要致电return Features.find({})。这是我的工作代码:
import { Features } from '../imports/api/features.js';
import '../imports/api/features.js'
Template.body.onCreated(function bodyOnCreated() {
Meteor.subscribe('features');
});
Template.body.helpers({
features: function() {
return Features.find({});
}
});
【问题讨论】:
标签: meteor meteor-blaze