流星;如何为新用户设置一个时间变量?

时间:2016-07-22 06:46:21

标签: javascript mongodb meteor

我想首次登录时设置某些变量,而我正在使用外部API。例如。在首次登录时,我们创建var得分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。

先谢谢!

3 个答案:

答案 0 :(得分:0)

我所看到的只是一个时间变量,所以无论你想做什么,我把我的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);




答案 1 :(得分: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.
});

答案 2 :(得分: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});
}
});