流星如何为新用户设置一次性变量

Meteor; How to set up one time variables for new users?

本文关键字:用户 设置 一次性 变量 新用户 流星      更新时间:2023-09-26

我想在第一次登录时设置某些变量,我使用的是外部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?

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

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美分放在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);

我遇到了类似的问题,所以我创建了一个名为"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});
}
});