0

i have an "back end" application which write in MongoDb (in database i have _id: with ObjectId("13f6ea...002")) i use meteor app to show information. Everything was good i displays list of information with {{#each}}. But when i wanted show one element with '_Id' nothing works.

I read this issue and adapt my code to get right root, But i can't display anything on the page. I tried to write Template helpers but it didn't helped

Db record:

  {
 _id: ObjectId("13f6ea...002"),
 url: "foo",
 title: "bar",
 published: "2014-08-22 03:26:21 UTC",
 image: "foo.jpg",
 summary: "foo ",
 categories: [
   "F",
   "B"
  ],
  ...
  }

Route:

this.route('news', {
path: '/news/:_id',
template: 'news',
waitOn: function () { 
  var id = this._id; 
  Meteor.subscribe('news', id);
},
data: function() {
  var id = this.params._id;
  return News.findOne({ _id: Meteor.Collection.ObjectID(this.params._id)});
},
 action : function () {this.render();},
});

Publish

Meteor.publish('news', function(id) {
  return News.find({_id: id}); 
});

Template which redirect to unique post

<h4><a href="{{pathFor 'news' _id=this._id.toHexString}}">{{title}}</a></h4>

And template is just {{news}}

How can i fix this?


UPDATE

My solutions to fix that:

router.js

waitOn: function () {
  var id = this._id;
  Meteor.subscribe('News', id);
},
data: function() {
  return News.findOne(new Meteor.Collection.ObjectID(this.params._id));
},

and in template

<a href="news/{{_id._str}}">

4

2 回答 2

1

在浏览器中导航到相应的 url(即localhost:3000/news/[_id]),打开控制台并输入:

Router.current().data()

这将向您显示当前路线的数据上下文。要么它什么也不返回,在这种情况下,您的查询存在根本问题,News.findOne因为它什么都不返回,或者(更有可能)它返回所需的文档。

在后一种情况下,据我所知,news该文档中没有任何属性,这就是它没有呈现任何内容的原因。如果您更改{{news}}为,{{url}}或者{{summary}}我想它会呈现请求的属性。

如果{{news}}您尝试渲染整个文档,那么(除了它将渲染为类似 的事实之外[Object])您需要创建news数据函数返回的对象的属性:

return {
    news: News.findOne({ _id: Meteor.Collection.ObjectID(this.params._id)});
};
于 2014-09-10T10:51:28.857 回答
0

使用 _id 获取文档:在事件下的 .js 文件中,点击事件和集合事件列表说:-

    'Submit form' : function () {
        var id = this._id;      
        return EventList.find({_id : id}).fetch();
    }

这将返回 id 的对象。在我的案例中,我正在为 Collection 中的所有文档显示一个字段。用户选择一条记录并单击提交,这将获取所有文档字段并显示给用户

于 2015-02-15T19:58:06.073 回答