meteor wont update data in mongodb

倾然丶 夕夏残阳落幕 提交于 2019-12-23 19:14:52

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/14028941/meteor-wont-update-data-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!