Can't update a mongo value with a dynamic array id from a meteor form

Peter Nunn

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.

Peppe L-G

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

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Meteor can't find by id in a Mongo.Collection

From Dev

Can't display data from Mongo in meteor using react

From Dev

Can't update value on the main form

From Dev

Can't update mongo array using RESTful api put

From Dev

Can't update a value in a Dictionary inside an Array?

From Dev

Can't update array value in mysql

From Dev

javascript : built an html form from json, but can't update the form

From Dev

Can't get option value from form

From Dev

Meteor Up deployment, can't use meteor mongo --url

From Dev

Can't connect to local (running) mongo using 'meteor mongo'

From Dev

React form doesn't update mongo database

From Dev

Meteor cannot update mongo?

From Dev

update value to dynamic associative array

From Dev

Update array element by id with mongo query

From Dev

Meteor/Mongo - add/update element in sub array dynamically

From Dev

Can't Update ObjectListView On Main Form From Class

From Dev

Can't update database entry from form fields

From Dev

Can't get value from multidimensional array

From Dev

Can't get value from multidimensional array

From Dev

Can't get value from an array (c)

From Dev

Can't print value from array of objects

From Dev

Find Value in Meteor Mongo

From Dev

Dynamic form class for getting value from a form

From Dev

Meteor dynamic form generation

From Dev

Meteor dynamic form

From Dev

I can't post value from dynamic dependents dropdown list

From Dev

Can't run "meteor mongo" on Meteor in Windows (latest meteor for windows exe bootstrapper)

From Dev

Update mongo array field from angular

From Dev

Can't start meteor app due to mongo exit