user1532669
user1532669

Reputation: 2378

What is wrong with my meteor leaderboard example?

I've just installed Meteor and I'm having a look at the leaderboard example.

I'm trying to set a random score, can't see what's wrong with my code. Can anyone see what's wrong with this? I'm expecting that when the button is clicked the selected player score is populated with random numbers.

Template code:

<template name="leaderboard">
<div class="leaderboard">
{{#each players}}
  {{> player}}
{{/each}}
</div>

{{#if selected_name}}
<div class="details">
<div class="name">{{selected_name}}</div>
 <input type="button" class="inc" value="Give 5 points" />
 <input type="button" class="incrandom" value="Set random points" />
</div>
{{else}}
<div class="none">Click a player to select</div>
{{/if}}
</template>

JS code:

  Template.leaderboard.events({
    'click input.incrandom': function () {
    Players.update(session.get("selected_player"), {$incrandom: {score: Math.floor(Random.fraction()*10)*5 }});
    }
  });

Upvotes: 0

Views: 73

Answers (1)

Tobias
Tobias

Reputation: 4074

There is no $incrandom operator in MongoDB, only $inc.

Also, what is Random in this code snippet? You probably meant to use Math.random().

Also (thank you @apendua, I didn't notice this), session should be Session.

So the updated code would be:

Template.leaderboard.events({
    'click input.incrandom': function () {
    Players.update(Session.get("selected_player"), {$inc: {score: Math.floor(Math.random()*10)*5 }});
    }
  });

Note, however, that this does not set the player's score to a random value but increments the player's score by a random value.

To set the player's score use the $set operator instead.

Upvotes: 1

Related Questions