这条通往user_profile的路线怎么了? [英] what is wrong with this route to user_profile?
问题描述
我正在尝试将配置文件页面添加到显微镜应用程序.多亏了我的帮助,我在此处可以使其中的大多数工作正常,但是我无法路由到另一个用户个人资料上班.这是路线的代码.谢谢
I am trying to add a profile page to the microscope app. Thanks to help I got here I was able to get most of it working but I can't get the route to another users profile to work. This is the code for the route. Thanks
在comment.html模板
in comment.html template
<span class="author"><a href="{{pathFor 'user_profile'}}">{{username}}</a></span>
router.js
router.js
this.route('user_profile',{
path: '/profile/:_username',
waitOn: function () {
return Meteor.subscribe('userprofile', this.params._username)
},
data: function () {return user.findOne(this.params._username)}
});
publications.js
publications.js
Meteor.publish('userprofile', function (username) {
return user.find(username);
});
profile.js
profile.js
Template.user_profile.helpers({
username: function() {
return this.user().username;
},
bio: function() {
return this.user().profile.bio;
}
});
推荐答案
accounts-base和accounts-password使用的默认Meteor用户集合是Meteor.users
,而不是user
.同样,collection.find(x)
将找到其_id
为x
的文档;如果要查找username
为x
的文档,则需要collection.find({username: x})
.
The default Meteor user collection used by accounts-base and accounts-password is Meteor.users
, not user
. Also, collection.find(x)
will find a document whose _id
is x
; if you want to find documents whose username
is x
, you need collection.find({username: x})
.
this.route('user_profile',{
path: '/profile/:username',
waitOn: function () {
return Meteor.subscribe('userprofile', this.params.username)
},
data: function () {return Meteor.users.findOne({username: this.params.username})}
});
我将_username
参数重命名为username
,这样pathFor
助手将能够自动填充它.我还用Meteor.users
替换了user
并传递了正确的选择器.>
I renamed the _username
parameter to username
, that way the pathFor
helper will be able to automatically fill it in. I also replaced user
with Meteor.users
and passed in the correct selector.
Meteor.publish('userprofile', function (username) {
return Meteor.users.find(
{username: username},
{fields: {username: 1, profile: 1}}
);
});
我用Meteor.users
替换了user
,并再次固定了选择器,并且限制了我们发布的字段(由于用户文档中包含诸如登录令牌之类的敏感数据,因此您不想发布整个内容). /p>
I replaced user
with Meteor.users
and fixed the selector again, and I limited which fields we publish (since the user document contains sensitive data like login tokens, you don't want to publish the whole thing).
Template.user_profile.helpers({
username: function() {
return this.username;
},
bio: function() {
return this.profile.bio;
}
});
在user_profile
模板内部,数据上下文(您在路由的data
参数中指定的)是用户文档,因此this
已经是用户文档.请注意,这些帮助程序是多余的(即使没有这些帮助程序,您也可以使用{{username}}
获取用户名,而使用{{profile.bio}}
获取bio).
Inside the user_profile
template, the data context (which you specified in the data
parameter in the route) is a user document, so this
is already a user document. Note that these helpers are redundant (you can get the username with {{username}}
and the bio with {{profile.bio}}
even without these helpers).
这篇关于这条通往user_profile的路线怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!