I've got a meteor event in which I'm trying to update one value in a number of sub documents from an event handler.
The string I'm putting together is order.0.number, order.1.number etc.. and I can see the correct strings being generated in the debugger. I can also manually update the field in the console, but the code stubbonly refuses to update the numbers.
The event code is
'click #closeEdit': function(evt,tmpl) {
evt.preventDefault();
Session.set('showEditEvent',false);
Session.set('lastMod', new Date());
Requests.update({_id:Session.get('editingReqEvent')}, {$set: {locked_by: null}});
var request = Requests.findOne({_id:Session.get('editingReqEvent')});
for (i=1; i <= request.order.length; i++) {
var val=tmpl.find('#'+i).value;
if(!val) {// its null
val = 0
}
var toSet = "order."+(i-1)+".number";
debugger;
Requests.update({_id:Session.get('editingReqEvent')}, {$set: {toSet: parseInt(val)}});
}
},
Any ideas how I should be doing this (obviously what I have here isn't the way to do it).
Thanks.
{toSet: parseInt(val)}
will create an object with the key toSet
. Not what you want, right? You need to do something like:
var toSet = "order."+(i-1)+".number"
var updates = {}
updates[toSet] = parseInt(val)
Requests.update(Session.get('editingReqEvent'), {$set: updates})
PS, in your code, create i
as a local variable instead of a global variable (guessing you're not using it as a global variable). DS
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments