Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 单个帖子订阅在我的meteor代码中不起作用_Mongodb_Meteor_Meteor Blaze_Flow Router - Fatal编程技术网

Mongodb 单个帖子订阅在我的meteor代码中不起作用

Mongodb 单个帖子订阅在我的meteor代码中不起作用,mongodb,meteor,meteor-blaze,flow-router,Mongodb,Meteor,Meteor Blaze,Flow Router,我只想根据传递的ID显示单个元素。我正在使用meteor的订阅和发布方法,也使用FlowRouter进行路由。当我尝试使用findOne获取数据并传递Id时,它不会返回任何数据,但当我找到({})时,它会获取所有数据并显示出来,不确定findOne为什么不工作 注意:我正在尝试根据MongoDB提供的对象ID(_ID)获取记录 posts = Mongo.collection("allPosts"); <Template name="stdSingleView"> {{#i

我只想根据传递的ID显示单个元素。我正在使用meteor的订阅和发布方法,也使用FlowRouter进行路由。当我尝试使用findOne获取数据并传递Id时,它不会返回任何数据,但当我找到({})时,它会获取所有数据并显示出来,不确定findOne为什么不工作

注意:我正在尝试根据MongoDB提供的对象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');
}
});
posts=Mongo.collection(“allPosts”);
{{{#if Template.subscriptionsReady}
{{{与学生历史一起}
{{id}}-{{name}
{{/与}}
{{else}
加载。。。。
{{/if}
Template.stdSingleView.onCreated(函数(){
var self=这个;
self.autorun(函数(){
var Id=FlowRouter.getParam('Id');
self.subscribe('singlePost',Id);
});
});
Template.stdSingleView.helpers({
studenthistory:function(){
var id=FlowRouter.getParam('id');
返回posts.findOne({u id:id});
}
});
if(Meteor.isServer){
Meteor.publish(“allposts”,function()){
返回posts.find({});
});
Meteor.publish('singlePost',函数(id){
检查(id、字符串);
returnposts.find({u-id:id});
});
}
pages.route(“/:id”{
名称:“singleView”,
操作:函数(参数){
BlazeLayout.render('stdSingleView');
}
});

find
将返回一个游标,该游标只包含一个文档。为了获取数据,您需要循环使用它,或者将helper更改为
findOne

Template.stdSingleView.helpers({
    studenthistory: function(){
        var id= FlowRouter.getParam('id');
        return posts.findOne({_id: id});
    }
});

当您使用_id完成findOne时,请将其包装到新的Mongo.ObjectID,然后传递它

请尝试以下代码:

  Meteor.publish('singleStudent', function(id) {
    check(id, String);
    return attendanceRegCol.find({"_id": new Mongo.ObjectID(id)});
  });

  Template.studentSingleView.helpers({
    studenthistory: function(){
      var id= FlowRouter.getParam('id');
      return attendanceRegCol.findOne({"_id": new Mongo.ObjectID(id)});
    }
});

谢谢Tran的回复。我使用的是findOne本身,但它不起作用。所以我把代码贴在这里的时候,我切换到了查找。findOne不起作用。最好的方法是尝试一步一步地调试。你能试着:打印出出版物上的
id
/打印出
posts.find({u id:id})
在出版物/页面上,打开
Chrome控制台
并键入
posts.find().fetch()
查看客户端上是否有任何数据?您好,我尝试过控制台,posts.find().fetch()。它给出的是空数组。已尝试打印文章。查找({u id:id});它显示了一个对象,选择器是“{5694f3869e8dfbde5d74438a}”,在服务器代码中,在发布方法中,我控制台了this->console.log(attendencegcol.find({u id:id}).fetch());即使这样,也会给出空数组。但是当我找到({})时,它会获取所有数据并显示出来,使用
{{{with studenthistory}}
?find对我也有效,但我需要findOne,因为我只需要显示一条不起作用的记录。请看这里的完整代码:谢谢Sharath!为我工作:)奇怪。。。不知道为什么我从不使用它,但它仍然适用于我的项目+1