Johannes
Johannes

Reputation: 335

meteor wont update data in mongodb

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

Upvotes: 3

Views: 1488

Answers (1)

Rouven Hurling
Rouven Hurling

Reputation: 119

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.

Upvotes: 3

Related Questions