Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hunting bug in Meteor collection update

Tags:

meteor

I’m trying out Meteor’s Leaderboard example and am running into a bug in trying to randomize the players’ scores.

The exception I’m hitting is Exception while simulating the effect of invoking '/players/update' undefined

The relevant code looks like this:

'click input.randomize_scores': function () {
  Players.find().forEach(function (player) {
    random_score = Math.floor(Math.random()*10)*5;
    Players.update(player, {$set: {score: random_score}})
  });
}

Full leaderboard.js contents here

I get the feeling I’m doing something pretty silly here. I’d really appreciate a pointer.

like image 411
Edward Ocampo-Gooding Avatar asked Apr 11 '12 05:04

Edward Ocampo-Gooding


1 Answers

The first argument to update() needs to be a document ID or a full Mongo selector. You're passing the complete player document. Try this:

Players.update(player._id, {$set: {score: random_score}});

which is shorthand for:

Players.update({_id: player._id}, {$set: {score: random_score}});
like image 150
debergalis Avatar answered Nov 16 '22 01:11

debergalis