PlayerList = new Mongo.Collection('players');
UerAccounts = new Mongo.Collection('user');
if(Meteor.isClient){
Template.leaderboard.helpers({
'player':function(){
var currentUserId = Meteor.userId();
return PlayerList.find({createdBy: currentUserId},{sort: {score: -1,name: 1}})
},
'selectedClass':function(){
var playerId = this._id;
var selectedPlayer = Session.get('selectedPlayer');
if(playerId == selectedPlayer){
return "selected"
}
},
'showSelectedPlayer':function(){
var selectedPlayer = Session.get('selectedPlayer');
return PlayerList.findOne(selectedPlayer)
}
});
Template.leaderboard.events({
'click .player': function(){
var playerId = this._id;
Session.set('selectedPlayer', playerId);
},
'click .increment':function(){
var selectedPlayer = Session.get('selectedPlayer');
PlayerList.update(selectedPlayer,{$inc:{score: 5}});
},
'click .decrement':function(){
var selectedPlayer = Session.get('selectedPlayer');
PlayerList.update(selectedPlayer,{$inc:{score: -5}});
},
'click .remove':function(){
var selectedPlayer = Session.get('selectedPlayer');
PlayerList.remove(selectedPlayer);
}
});
Template.addPlayerForm.events({
'submit form':function(event){
event.preventDefault();
var playerNameVar = event.target.playerName.value;
Meteor.call('insertPlayerData');
}
});
Meteor.subscribe('thePlayers');
Meteor.methods({
'insertPlayerData': function(){
var currentUserID = Meteor.userId();
PlayerList.insert({
name:"David",
score:0,
createdBy:currentUserId
});
}
});
}
if(Meteor.isServer){
Meteor.publish('thePlayers',function(){
var currentUserId = this.userId;
return PlayerList.find({createdBy: currentUserId})
});
}
<head>
<title>Leaderboard</title>
</head>
<body>
<h1>Leaderboard</h1>
{{> leaderboard}}
{{> loginButtons}}
</body>
<template name="leaderboard">
<ul>
{{#each player}}
<li class="player {{selectedClass}}">{{name}}: {{score}}</li>
{{/each}}
</ul>
<input type="button" class = "increment" value ="give five points">
<input type="button" class = "decrement" value ="take five points">
<input type="button" class = "remove" value="remove player">
{{#if showSelectedPlayer}}
<li>slected Player: {{showSelectedPlayer.name}}</li>
{{/if}}
{{> addPlayerForm}}
</template>
<template name="addPlayerForm">
{{#if currentUser}}
<form>
<input type="text" name="playerName">
<input type="submit" value="Add Player">
</form>
{{/if}}
</template>
I am just starting to explore meteor and learning about methods and security concerns. While testing my code, I encountered an error stating "Exception while simulating the effect of invoking 'insertPlayerData'. Reference Error: currentUserId is not defined.".