【问题标题】:what is wrong with this route to user_profile?这条通往 user_profile 的路线有什么问题?
【发布时间】:2014-10-07 22:33:23
【问题描述】:

我正在尝试向显微镜应用程序添加个人资料页面。感谢帮助我得到了here 我能够让它大部分工作,但我无法获得另一个用户配置文件的路径来工作。这是路线的代码。谢谢

在comment.html模板中

<span class="author"><a href="{{pathFor 'user_profile'}}">{{username}}</a></span>

路由器.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;
  }
});

【问题讨论】:

  • user 集合是什么?默认 Meteor 用户集合是Meteor.users
  • 我认为是默认的accounts_base和accounts_password。我对此很陌生

标签: javascript html meteor iron-router


【解决方案1】:

accounts-base 和 accounts-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 助手将能够自动填充它。我还将user 替换为Meteor.users 并传入正确的选择器。

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}} 获取个人简介)。

【讨论】:

  • 如何在个人资料中添加更多字段以发布。
猜你喜欢
  • 1970-01-01
  • 2012-10-20
  • 2011-04-15
  • 1970-01-01
  • 2020-02-14
  • 2014-11-26
  • 2016-06-04
  • 1970-01-01
  • 2016-05-08
相关资源
最近更新 更多