3

I have this code:

Nodes = new Meteor.Collection("nodes");
[...]
Template.list.events({
  'click .toggle': function () {
      Session.set("selected_machine", this._id);
      Nodes.update(Session.get("selected_machine"), {$set: {"conf" :{"flag": true }}});
  }
});

I can't convince meteor to update my entry. Theres a microsecond flash in the DOM, but the server rejects to update.

This is my data: { "_id" : ObjectId("50d8ec4f5919ffef343c9151"), "conf" : { "flag" : false }, "name" : "sepp" }

console.log(Session.get("selected_machine")); shows me the id. Insecure Package is installed. Writing by hand in the minimongo console works as expected.

Is there a Problem because I wan't to update a subarray? What am I doing wrong? Thank you for help

Johannes
  • 337
  • 1
  • 4
  • 17

1 Answers1

3

This is because your data uses the MongoDB ObjectId, it's a known issue that Meteor can't update these values (https://github.com/meteor/meteor/issues/61).

you could run this hack in the mongo shell (meteor mongo) to fix it (credit to antoviaque, i just edited it for your collection)

db.nodes.find({}).forEach(function(el){
    db.nodes.remove({_id:el._id}); 
    el._id = el._id.toString(); 
    db.nodes.insert(el); 
});

Meteor sees the ObjectId just as a string and because of that MongoDB doesn't find something to update. It works client-side, because in your local collection these _id's are converted to strings.

For experimenting you should insert data via the browser console and not via the mongo shell, because then Meteor generates UUID's for you and everything is (and will be) fine.

PS: I ran into the same problem when I started on my App.

  • 1
    Well done! Solved my issue in 2 seconds -- I spent the whole night on this. Maybe i've overseen it - but a big fat warning in the meteor docs wouldn't be to much to ask to the meteor folks. Probably **every** new meteor user will fall exactly in the same trap. When I try to figure out a framework I always wan't to see what comes out in the database. Thank you Rouven! – Johannes Dec 25 '12 at 15:46
  • `.toString()` converts it to a string literal `ObjectId(...)` in some versions of mongo. Safer to use `_id.str` – ChatGPT Nov 16 '14 at 14:50