1

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.)

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.