Search code examples
mongodbmeteor

Store Variable in Session Meteor


Can anyone fix the code below so the variable nickname is stored in the session?

Template.nicknameForm.events({
    'click .js-set-nickname':function(){

        Session.set(nickname);

        var nickname = $('#nickname-input').val();

    }    
});

Many thanks in advance


Solution

  • https://docs.meteor.com/api/session.html:

    Template.nicknameForm.events({
        'click .js-set-nickname':function(){
            var nickname = $('#nickname-input').val();
            Session.set('nickname', nickname);
        }    
    });
    

    Note that you seem to be misunderstanding how Session works. So please read the docs. In particular, be aware that there is no such thing as "storing a variable in the session". You can't just refer to the variable nickname to retrieve the value. You'll need to use const nickname = Session.get('nickname') to restore it on a page-load.