2016-07-22 70 views
0

我想在第一次登录时设置某些变量,而我正在使用外部API。例如。仅在第一次登录时,我们创建var score 0,以及一系列与配置文件相关的不同变量。流星;如何为新用户设置一个时间变量?

Accounts.onLogin(function() { 
     // To retrieve more details about user 
     var steamApiKey = ("XXXXXXXXXXXXXXXXXXXX"); //Steam API KEY 
     var steam64Id = Meteor.user().profile.id; //User's Steam 64 ID 
     var result = Meteor.http.get('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' + steamApiKey + '&steamids=' + steam64Id); 

       Meteor.users.update(Meteor.userId(), // Steam Nickname 
        { $set: { 'profile.personaname': result.data.response.players[0].personaname}} 
        ); 
       Meteor.users.update(Meteor.userId(), // Steam Avatar 
        { $set: { 'profile.steamAvatar': result.data.response.players[0].avatarmedium}} 
        ); 
       Meteor.users.update(Meteor.userId(), 
        { $set: { 'profile.score': "0"}} 
        ); 
    }); 

我只有这一点,我意识到它会随时更新为0,每次我登录即使寿我手动更新得分为100

在先进的感谢!

回答

0

尝试检查首次登录时创建的字段是否已经存在。

Accounts.onLogin(function() { 
    let user = Meteor.user(); 
    if (user.profile.hasOwnProperty("personaname")) return; 

    // To retrieve more details about user 
    var steamApiKey = ("XXXXXXXXXXXXXXXXXXXX"); //Steam API KEY 
    // ...and so on. 
}); 
+0

非常感谢您的工作。我不得不调整,以满足我的需求,但我认为*我得到了我需要的东西。欣赏它! – Farhan

0

我看到的是一个时间变量,因此,无论你的努力做IMA把我的2美分左右的localStorage所以这里去

var score = 0; 
 

 
function Start(){ 
 
    //if localStorage does not have key 
 
    if(!localStorage.hasKey("score")){ 
 
    localStorage.setItem("score",0); 
 
    } 
 
}

所以让我们来谈谈一些装载和节能功能

var score = 0; 
 

 
function Start(){ 
 
    Load(); 
 
} 
 

 
function Save(){ 
 
    localStorage.setItem("score",score); 
 
} 
 

 
function Load(){ 
 
    var score_save = localStorage.getItem("score") || "0"; 
 
    
 
    score = parseInt(score_save); 
 
} 
 

 
//Once everything is initialized start 
 
Start(); 
 

 
//Save every second 
 
setInterval(Save,1000);

0

我有类似的问题,所以我创建了一个名为“userprofile”的集合。 “userprofile”集合中文档的_id将与用户集合_id相同。这样我们在userprofile文档中创建引用。

Suppose user collection _id ='1234' then we will save userprofile document with _id:'1234' 

'findUser':function() { 


// Make sure the user is logged in before inserting a task 
if (! this.userId) { 
    throw new Meteor.Error('not-authorized'); 
} 

var userProfile = userprofile.findOne(
{ _id: this.userId}); 

if(userProfile===null ||userProfile===undefined) { 
    userprofile.insert({_id:this.userId}); 
} 
});