我正在尝试显示保姆的独特形象(即:保姆用户名、城市、邮政编码等)。来自一个名为“保姆”的架构/集合。
URL正确地包含了保姆唯一的_id,例如:
http://localhost:3000/test/PqMviBpYAmTA2b5ec
但我没办法找回所有其他的字段。
流星问题-截图
我尝试在两个文件中查询MongoDB : routes.js和模板test.js
1)在routes.js中
Router.route('/test/:_id', {
name: 'test',
data: function () {
return Babysitters.findOne({ _id: this.params._id });
}
});2)在test.js中
Template.test.helpers({
data: function () {
//sitterusername = this.params.sitterusername;
//console.log(this.params._id );
return Babysitters.findOne( { _id: this.params._id });
}
});html文件: test.html
<template name="test">
{{#with data}}
<ul>
<li><img src="/" {{photourl}} height="100" width="100" ></li>
<li>Babysitter username: {{ sitterusername }}</li>
<li>Presentation: {{ presentation }}</li>
<li>City: {{ city }}</li>
<li>Postal Code: {{ postalcode }}</li>
<li>Mother tongue: {{ mothertongue }}</li>
<li>Languages spoken {{ languagesspoken }}</li>
<li>Experience {{ experience }}</li>
<li>Homework help: {{ homeworkhelpavailable }}</li>
<li>Hourly wages: {{ hourlywages }} €/h</li>
</ul>
{{/with}}
</template>我尝试过各种方法,但集合字段从未出现在HTML文件中。
谢谢你的帮助,这里有个新手。
K.
发布于 2016-04-01 18:21:30
很可能没有将所有Babysitters发布到客户端,因此.findOne()没有返回任何内容。
这是一个常见的路由器模式,您希望显示一个通常不发布的文档。在is中解决这一问题的一个好方法是对单个文档进行waitOn订阅:
waitOn: function(){
return Meteor.subscribe('oneBabysitter', this.params._id);
}在服务器上发布:
Meteor.publish('oneBabysitter',function(_id){
return Babysitters.find(_id);
});注意,即使这个发布只返回一个文档,您仍然必须执行一个.find(),而不是一个.findOne(),因为发布需要返回游标或游标数组,而不是对象。
https://stackoverflow.com/questions/36353752
复制相似问题