2016-02-04 86 views
1

我有一個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,但是返回當前登錄的勝利而不是擁有該個人資料頁面的用戶的勝利。

回答

2

所有用戶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); 
} 
+0

我如何獲得該特定個人資料頁的用戶id? –

+0

你是如何認識到**是誰的簡介? –

+0

您的帖子中沒有用戶名字,但是沒問題。而不是'Meteor.users.findOne(userId)'通過用戶名:'Meteor.users.findOne({username:username})'找到你的用戶。 –