会话变量在Meteor中未定义

时间:2015-08-12 11:50:07

标签: javascript mongodb meteor meteor-helper

我试图在'会话中执行某些程序。我创建了一个名为' leaderboard'。

的项目

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

这是我的代码:

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>

1 个答案:

答案 0 :(得分:0)

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