简体   繁体   中英

Session variable is undefined in Meteor

While I was trying to execute some programs on 'sessions'. I have created a project named 'leaderboard'.

Executing this leaderboard shows some 'undefined' console log. Any idea why?

Here is my code:

PlayerList = new Mongo.Collection('players');
if (Meteor.isClient) {
    Template.leaderboard.helpers({
        'player': function() {
            return PlayerList.find()
        },

    });
    Template.leaderboard.events({
        'click .player': function() {
            var playerId = this._id;
            Session.set('selectedPalyer', playerId);
            var selectedPlayer = Session.get('selectedPlayer');
            console.log(selectedPlayer);
        }

    });
}

<head>
    <title>Leaderboard</title>
</head>
<body>
    <h1>Leaderboard</h1>
    {{> leaderboard}}
</body>

<template name="leaderboard">
     <ul>
         {{#each player}}
         <li class="player">{{name}}: {{score}}</li>
         {{/each}}
     </ul> 
</template>

You have a typo in your Session key. Just replace Session.set('selectedPalyer', playerId); with Session.set('selectedPlayer', playerId); and you are ready to go:

PlayerList = new Mongo.Collection('players');
if (Meteor.isClient) {
    Template.leaderboard.helpers({
        'player': function () {
            return PlayerList.find();
        }
    });
    Template.leaderboard.events({
        'click .player': function () {
            var playerId = this._id;
            Session.set('selectedPlayer', playerId);
            var selectedPlayer = Session.get('selectedPlayer');
            console.log(selectedPlayer);
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM