Updating Profile collection with custom fields overwrites data

First of all, thanks for reading! I am struggling with this. For my site, I have 3 signup forms each with their own function/submit button that sends data to the profile collection. Each button works individually and updates their “profile” with the new data, however, the data submitted previously for the other “forms” disappears. I realize this is a lot to look at so I apologize in advance but appreciate the help!

Template.signup1.events({
        'click .signup1-continue-button': function(event, template){
            event.preventDefault();
//
            //PROFILE
            var username   = $('[name=username]').val();
            var password   = $('[name=password]').val();
            var email      = $('[name=email]').val();
            var age        = $('[name=age]').val();
            var name       = $('[name=name]').val();
            var timezone   = $("[name=timezone] option:selected").text();
//
            Accounts.createUser({
                email: email,
                password: password,
                username: username,
                profile:{
                        age: age,
                        name:name,
                        timezone:timezone
                }}
            },//end submitProfile function

        'click .submitHalo': function(event) {
            event.preventDefault();
//
            //HALO
            var halo_gamerTag = $('[name=halo_gamerTag]').val();
            var halo_ratio = $('[name=halo_ratio]').val();
            var halo_position = $("[name=halo_position] option:selected").text();
            var halo_gametype = $("[name=halo_gametype] option:selected").text();
            var halo_rank = $("[name=halo_rank] option:selected").text();
            var halo_dayM = $("#halo_dayM").is(":checked");
            var halo_dayT = $("#halo_dayT").is(":checked");
            var halo_dayW = $("#halo_dayW").is(":checked");
            var halo_dayTH = $("#halo_dayTH").is(":checked");
            var halo_dayFR = $("#halo_dayFR").is(":checked");
            var halo_daySA = $("#halo_daySA").is(":checked");
            var halo_daySU = $("#halo_daySU").is(":checked");
            var halo_nightDay = $('[name=halo_nightDay]').val();
            var halo_fromtime = $('[name=halo_fromtime]').val();
            var halo_fromtime_span = $("[name=halo_fromtime_span] option:selected").text();
            var halo_totime = $('[name=halo_totime]').val();
            var halo_totime_span = $("[name=halo_totime_span] option:selected").text();
            var halo_time = halo_fromtime+""+halo_fromtime_span+"-"+halo_totime+""+halo_totime_span;
            var halo_exp_years = $('[name=halo_exp_years]').val();
            var halo_exp_months = $('[name=halo_exp_months]').val();
            var halo_tournament = $("#halo_tournament").is(":checked");
            var user_id = Meteor.userId();
            Meteor.users.update(user_id, {$set: {profile: {
                halo:{gamertag:halo_gamerTag , position: halo_position, gametype: halo_gametype, rank: halo_rank, day: 
                    {M: halo_dayM,T: halo_dayT, W: halo_dayW, TH: halo_dayTH, FR: halo_dayFR, SA: halo_daySA, SU: halo_daySU}, 
                    time:halo_time , nightDay:halo_nightDay, halo_ratio: halo_ratio, experience: halo_exp_years+"years "+halo_exp_months+"months"}}}
    });
},
        'click .submitDota': function(event) {
            event.preventDefault();
//
            //DOTA
            var dota2_position = $("[name=dota2_position] option:selected").text();
            var dota2_mmr = $('[name=dota2_mmr]').val();
            var dota2_games = $('[name=dota2_games]').val();
            var dota2_hoursplayed = $("#dota2_hoursplayed").is(":checked");
            var dota2_gamesplayed = $("#dota2_gamesplayed").is(":checked");
            var dota2_hero1 = $('[name=dota2_hero1]').val();
            var dota2_hero2 = $('[name=dota2_hero2]').val();
            var dota2_hero3 = $('[name=dota2_hero3]').val();
            var dota2_winrate = $('[name=dota2_winrate]').val();
            var dota2_server = $("[name=dota2_server] option:selected").val();
            var dota2_steamid = $('[name=dota2_steamid]').val();
            var user_id = Meteor.userId();
            Meteor.users.update(user_id, {$set: {profile: {
                dota2:{position: dota2_position, mmr: dota2_mmr, hoursGames:dota2_games, 
                    heroes:{first: dota2_hero1, second: dota2_hero2, third: dota2_hero3}, 
                    winRate: dota2_winrate, region: dota2_server, steamID:dota2_steamid}}}
                });
},
        'click .submitLol': function(event) {
            event.preventDefault();
//
            //LOL
            var lol_position = $("[name=lol_position] option:selected").text();
            var lol_rank = $("[name=lol_rank] option:selected").text();
            var lol_champ1 = $('[name=lol_champ1]').val();
            var lol_champ2 = $('[name=lol_champ2]').val();
            var lol_champ3 = $('[name=lol_champ3]').val();
            var lol_gameplayed = $('[name=lol_gameplayed]').val();
            var lol_preferredserver = $('[name=lol_preferredserver]').val();
            var lol_summonerName = $('[name=lol_summonerName]').val();
            var user_id = Meteor.userId();
            Meteor.users.update(user_id, {$set: {profile: {
                lol:{position: lol_position, rank: lol_rank, champions: {first:lol_champ1 ,second:lol_champ2 ,third:lol_champ3}, 
                games:lol_gameplayed, comms:lol_preferredserver, lol_summonerName:lol_summonerName}}}
    });
}
});

As a heads up, wrap triple backticks around your code to format it.

Anyway, when you do this:

Meteor.users.update(user_id, {
  $set: {
    profile: {
      dota2: {
        position: dota2_position,
        mmr: dota2_mmr,
        hoursGames: dota2_games,
        heroes: { first: dota2_hero1, second: dota2_hero2, third: dota2_hero3 },
        winRate: dota2_winrate,
        region: dota2_server,
        steamID: dota2_steamid
      }
    }
  }
});

you are setting the ‘profile’ as the new data, which is overwriting what you set in step one. Try this instead:

Meteor.users.update(user_id, {
  $set: {
    'profile.dota2': {
      position: dota2_position,
      mmr: dota2_mmr,
      hoursGames: dota2_games,
      heroes: { first: dota2_hero1, second: dota2_hero2, third: dota2_hero3 },
      winRate: dota2_winrate,
      region: dota2_server,
      steamID: dota2_steamid
    }
  }
});

That way, you are setting only the ‘profile.dota2’ information.

2 Likes

Also, it looks like you’re only writing non-sensitive data, but there is a short blurb here about why using profile is not recommended.

Thank you so much for that! I was really struggling. I will definitely think about switching away from profile. Again, thanks a lot! :slight_smile:

1 Like