【问题标题】:Access profile page by username or id通过用户名或 ID 访问个人资料页面
【发布时间】:2015-07-16 00:31:57
【问题描述】:

我在 Meteor 中使用用户系统。记帐是通过电子邮件创建的,但我想为用户提供在其设置中设置唯一用户名的选项。

我有这条路线

Router.route('userProfile', {
  path: '/users/:userId',
  template: 'userProfile',
  fastRender: true,
  waitOn: function() {
    return Meteor.subscribe('singleUser', this.params.userId);
  },
  data: function() {
    return {
      user: Meteor.users.findOne(this.params.userId)
    };
  }
});

但是如果用户设置了他/她的用户名,路由路径应该是path: '/:username',。我认为这也是 Facebook、LinkedIn 等的做法。

【问题讨论】:

    标签: javascript node.js meteor iron-router


    【解决方案1】:

    只需选择单个参数并在您的发布和数据函数中检查它:

    Meteor.publish('singleUser', function (userIdOrName) {
      return Meteor.users.find({ $or: [ { _id: userIdOrName }, { username: userIdOrName } ] }, {limit: 1});
    });
    
    Router.route('userProfile', {
      path: '/users/:userIdOrName',
      template: 'userProfile',
      fastRender: true,
      waitOn: function() {
        return Meteor.subscribe('singleUser', this.params.userIdOrName);
      },
      data: function() {
        return {
          user: Meteor.users.findOne({ $or: [ { _id: this.params.userIdOrName }, { username: this.params.userIdOrName } ] })
        };
      }
    });
    

    请注意,在这种情况下,您应该确保用户不能将其他人的 ID 设置为他或她的用户名,否则他/她可能会冒充此人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-31
      • 2015-05-03
      • 2020-01-23
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多