模拟调用" insertPlayerData'的效果时出现异常ReferenceError:未定义currentUserId

时间:2015-08-19 17:27:53

标签: javascript mongodb meteor



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 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>
     
&#13;
&#13;
&#13;

我是流星现在学习方法和安全问题的新手。 当我执行我的代码时,它正在显示&#34;异常,同时模拟调用&#39; insertPlayerData&#39;参考错误:currentUserId未定义&#34;错误。

1 个答案:

答案 0 :(得分:3)

此Meteor方法应该在服务器上,而不是客户端上。您还错过了Meteor.call('insertPlayerData')语句的回调。我清理了其他一些东西(Meteor.subscribe不应该漂浮在这样的空白区域。)

以下新代码(未经测试):

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