javascript - Exception while simulating the effect of invoking 'insertPlayerData' ReferenceError: currentUserId is not defined -
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 currentuerid = meteor.userid(); playerlist({ 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 5 points"> <input type="button" class = "decrement" value ="take 5 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 new meteor learning methods , security issues . while executing code showing "exception while simulating effect of invoking 'insertplayerdata' reference error: currentuserid not defined" error .
this meteor method should on server, not client. you're missing callback meteor.call('insertplayerdata')
statement. cleaned couple other things (meteor.subscribe
should not floating in empty space that).
new code below (untested):
playerlist = new mongo.collection('players'); useraccounts = new mongo.collection('user'); if (meteor.isclient) { template.leaderboard.oncreated(function () { this.subscribe('theplayers'); }); 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', function (error, result) { // handle error/success }); } }); } if (meteor.isserver) { meteor.publish('theplayers', function () { var currentuserid = this.userid; return playerlist.find({createdby: currentuserid}) }); meteor.methods({ 'insertplayerdata': function () { var currentuserid = meteor.userid(); playerlist.insert({ name: "david", score: 0, createdby: currentuserid }); // need error handling return {success: true}; } }); }
Comments
Post a Comment