会话变量在 Meteor 中未定义

Session variable is undefined in Meteor

当我试图在 'sessions' 上执行一些程序时。我创建了一个名为 'leaderboard'.

的项目

执行此排行榜会显示一些 'undefined' 控制台日志。知道为什么吗?

这是我的代码:

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>

您的 Session 键有错字。 只需将 Session.set('selectedPalyer', playerId); 替换为 Session.set('selectedPlayer', playerId); 即可开始:

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