这个到user_profile的路由有什么问题?
what is wrong with this route to user_profile?
我正试图添加一个配置文件页面到显微镜应用程序。多亏了我在这里的帮助,我能够得到它的大部分工作,但我无法得到路由到另一个用户的配置文件工作。这是路线的代码。由于
在comment.html模板
<span class="author"><a href="{{pathFor 'user_profile'}}">{{username}}</a></span>
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
Meteor.publish('userprofile', function (username) {
return user.find(username);
});
profile.js
Template.user_profile.helpers({
username: function() {
return this.user().username;
},
bio: function() {
return this.user().profile.bio;
}
});
account -base和account -password使用的默认Meteor用户集合是Meteor.users
,而不是user
。同样,collection.find(x)
将找到_id
为x
的文档;如果你想找到username
是x
的文档,你需要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
,并传入了正确的选择器。
Meteor.publish('userprofile', function (username) {
return Meteor.users.find(
{username: username},
{fields: {username: 1, profile: 1}}
);
});
我用Meteor.users
替换了user
,并再次固定了选择器,并且我限制了我们发布的字段(因为用户文档包含敏感数据,如登录令牌,您不想发布整个内容)。
Template.user_profile.helpers({
username: function() {
return this.username;
},
bio: function() {
return this.profile.bio;
}
});
在user_profile
模板中,数据上下文(在路由中的data
参数中指定)是一个用户文档,因此this
已经是一个用户文档。注意,这些帮助是多余的(即使没有这些帮助,您也可以使用{{username}}
获得用户名,使用{{profile.bio}}
获得个人信息)。
相关文章:
- 这是什么 ==- javascript 运算符
- Emberjs应用程序加载在除Index之外的所有路由上
- 我的单元测试选项是什么
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- 什么'在Express中路由时,应用程序级中间件和路由器级中间件之间的区别是什么
- 在角度路由模块中使用“解析”有什么好处
- 在单页应用程序上进行客户端路由的正确方法是什么
- 使用ui路由器实现动态URL路由的最佳方式是什么
- ng路由不起作用.我不知道错误是什么,因为控制台中没有显示任何内容
- 将 /.json 添加到快速路由末尾的最佳方法是什么
- JavaScript 路由能给你带来什么
- 在backbonej中触发路由的最佳方式是什么
- “窗口”的真正含义是什么?在Meteor.js应用中,onload '用于铁路由
- 这个到user_profile的路由有什么问题?
- 用ember.js重新加载非动态路由模型的正确方法是什么?
- 使用Angular JS,在路由改变时关闭web套接字连接的正确方法是什么?
- 在React-Router文档中,“索引路由”中的这一段是什么意思?部分
- 在expressJS中处理路由的最佳实践是什么?
- 如果你用express js重载同一个路由会发生什么?
- AngularJS使用路由参数进行客户端包含的方式是什么