无法为各个项目制作可点击链接:
router.js:
Router.route('/projects/:_id', {
name: 'projectPage',
template: 'projectPage',
data: function() {
return Projects.findOne({ _id: this.params._id })
}
})
project_list.html:
<template name="projectList">
<div class="col-md-10">
<div class="row">
<h2 class="page-header">My Lessons</h2>
{{#each projects}}
{{> project}}
{{/each}}
</div>
</div>
</template>
project.html:
<template name="project">
<div class="col-md-4">
<div class="thumbnail">
<img src="thumb-1.png" alt="...">
<div class="caption">
<h3>{{title}}</h3>
<p>{{description}}</p>
<p><a href="{{pathFor 'projectPage'}}" class="btn btn-primary" role="button">Check out</a></p>
</div>
</div>
</div>
</template>
project_page.html:
<template name="projectPage">
<div class="col-md-10">
<div class="row">
<h2 class="page-header">{{title}}</h2>
<h2 class="page-header">{{description}}</h2>
</div>
</div>
</template>
我收到了这个奇怪的网址:http://localhost:3000/projects/ObjectID(%22566b810371d07711cf9d6680%22)
我被带到了projectPage
,但没有任何表现。
我做错了什么?
答案 0 :(得分:2)
问题在于,铁路由器在您的商品ObjectID
中看到_id
,预计会找到String
。您可以使用以下代码,将您的ObjectID转换为即时字符串:
<强> project.html:强>
<template name="project">
<div class="col-md-4">
<div class="thumbnail">
<img src="thumb-1.png" alt="...">
<div class="caption">
<h3>{{title}}</h3>
<p>{{description}}</p>
<p><a href="{{pathFor 'projectPage' _id=this._id.toHexString}}" class="btn btn-primary" role="button">Check out</a></p>
</div>
</div>
</div>
</template>
但是你必须将它转换回路线中的ObjectID:
<强> router.js:强>
Router.route('/projects/:_id', {
name: 'projectPage',
template: 'projectPage',
data: function() {
return Projects.findOne({ _id: new Meteor.Collection.ObjectID(this.params._id) })
}
})