这个到user_profile的路由有什么问题?

what is wrong with this route to user_profile?

本文关键字:路由 什么 问题 profile user      更新时间:2023-09-26

我正试图添加一个配置文件页面到显微镜应用程序。多亏了我在这里的帮助,我能够得到它的大部分工作,但我无法得到路由到另一个用户的配置文件工作。这是路线的代码。由于

在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)将找到_idx的文档;如果你想找到usernamex的文档,你需要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}}获得个人信息)。