Reputation: 4415
In a Meteor application I have an event that should update the database data for changed fields on a form.
So the event handler looks like this:
Template.someTemplate.events({'change #someForm' : function(){
eval("someCollection.update(Session.get(\"currentId\"), {$set: {" + event.target.name + ":event.target.value}});");
}})
This works fine, when I use eval. But I doubt it is considered a best practice to use eval like this, or is it?
How would I have to write the MongoDB-update code, to make something like the following work?
Template.someTemplate.events({'change #someForm' : function(){
var variableName = event.target.name;
someColletion.update(Session.get("currentId"), {$set: {variableName:event.target.value}});
}})
Or what is the right way, to approach this? (The code above does not work, as for the MongoDB update instruction the variableName is not evaluated, so "variableName" is passed instead of its set value.
Note that I want to use this for prototyping, not actually a real application. So security is actually not really an issue. (I am a UX guy, not a software engineer.)
Upvotes: 1
Views: 548
Reputation: 887479
You need to create the object using indexer notation:
var param = { $set: {} };
param.$set[event.target.name] = event.target.value;
someCollection.update(Session.get("currentId"), param);
Upvotes: 3