【发布时间】:2016-04-24 17:28:51
【问题描述】:
我想根据传递的 ID 仅显示单个元素。我正在使用流星的订阅和发布方法,同样使用 FlowRouter 进行路由。当我尝试使用 findOne 获取数据并传递 Id 时,它不会返回任何数据,但是当我执行 find({}) 时,它会获取所有数据并显示它,不知道为什么 findOne 不起作用..
注意:我正在尝试根据 MongoDB 提供的 Object ID(_id) 获取记录。
posts = Mongo.collection("allPosts");
<Template name="stdSingleView">
{{#if Template.subscriptionsReady}}
{{#with studenthistory}}
{{id}} - {{name}}
{{/with}}
{{else}}
Loading....
{{/if}}
</Template>
Template.stdSingleView.onCreated(function(){
var self = this;
self.autorun(function(){
var Id = FlowRouter.getParam('id');
self.subscribe('singlePost', Id);
});
});
Template.stdSingleView.helpers({
studenthistory: function(){
var id= FlowRouter.getParam('id');
return posts.findOne({_id: id});
}
});
if (Meteor.isServer) {
Meteor.publish("allposts", function() {
return posts.find({});
});
Meteor.publish('singlePost', function(id) {
check(id, String);
return posts.find({_id: id});
});
}
pages.route( '/:id', {
name: 'singleView',
action: function( params ) {
BlazeLayout.render('stdSingleView');
}
});
【问题讨论】:
-
你怎么能
but when i do find({}), it gets all the data and displays it,和{{#with studenthistory}}? -
find 也对我有用,但我需要做 findOne,因为我只需要显示一条记录,这是行不通的。请在此处查看完整代码:jsfiddle.net/3s6zL5hg/1
标签: mongodb meteor meteor-blaze flow-router