【问题标题】:Meteor: Get user information from user profile pageMeteor:从用户个人资料页面获取用户信息
【发布时间】:2016-05-13 17:05:58
【问题描述】:

我有一个 Meteor 项目,为每个用户设置了个人资料页面系统,这是我在 question 的帮助下创建的。

如何在我的 Template.helpers 的 JS 代码中从该页面的用户个人资料中检索信息?

例如。我的个人资料页面有以下助手

Template.profile.helpers({
  winLoseRatio: function() {
    var wins = 0;
    var losses = 0;

    var ratio = wins/losses;
    ratio = Math.round((ratio * 1000) + 0.000001) / 1000;

    return ratio;
  },
  totalGuesses: function() {
    var wins = 0;
    var losses = 0;

    var total = wins + losses;

    return total;
  }
});

对于所有 0 的值,我想获取用户(我所在的个人资料)的 .profile.stats.wins 和 .profile.stat.losses 字段值。

我尝试了Meteor.userId().profile.stats.wins,但返回的是当前登录的您的胜率,而不是拥有个人资料页面的用户的胜率。

【问题讨论】:

    标签: meteor field router helpers


    【解决方案1】:

    Meteor.users 集合中的所有用户都可用,所以:

    // Find user with id of currently viewed profile.
    var user = Meteor.users.findOne(userId);
    if (user) {
        console.log(user.profile.stats.wins);
    } else {
        console.log('No user with id', user);
    }
    

    【讨论】:

    • 如何获取该特定个人资料页面的 userId?
    • 您的帖子中没有 username 字样,但没关系。而不是 Meteor.users.findOne(userId) 通过用户名查找您的用户:Meteor.users.findOne({ username: username })
    • 好的,抱歉 - 我刚起床。第二个findOne 工作了吗?
    • 我在客户端控制台中收到用户名未定义错误。
    • 好的,我必须将用户名作为参数传递给函数。感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 2018-08-04
    • 2014-11-22
    • 1970-01-01
    相关资源
    最近更新 更多