Search code examples
javascriptmongodbmeteor

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


I'd like to set certain variables upon first login, and I am using an external API. e.g., upon only first login, we create var score 0, and a bunch of different variables tied to the profile.

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"}}
        );
});

I only have this. Why will it always update to 0 everytime I log in even though I manually updated score to 100?


Solution

  • Try checking whether the fields created at first login already exist or not.

    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.
    });