Dynamic array update, why won't this update?


#1

Hi Meteorites,

I’ve been trying to update a nested array inside of a document, my update method looks like this:

updateLine: function (taskId, name) {
    var setObj = {};
    setObj.$set = {};
    var key = 'line.' + index;
    setObj.$set[key] = {
        name: name
    };
    console.log(setObj);
    tasks.update({_id: taskId}, setObj);
},

When I console.log the setObj, it looks like it’s supposed to, like this:

{'$set': {'line.0': {name: name}}

But it doesn’t actually update, nor am I getting errors. The only thing I can think of is that the quotes don’t look quite right ($set shouldn’t be in quotes). On the server it returns as above, on the client both $set and line.0 return without quotes.

I’ve been hitting my head against a wall for a while, here, I’m sure it’s something simple I’m overlooking.


#2

You want { 'line.0.name': name }


#3

Thanks, that was a step in the right direction.

It would have helped if taskId was defined!

:sweat_smile:


#4

Looks like you already got help but I just wanted to point out that you should consider checking and validating the taskId field before you pass it directly to a DB query to avoid injection attacks.


#5

@Sparser Do you have anything in particular in mind, or just a check() function per here: http://docs.meteor.com/#/full/check


#6

Check will do the job, I believe.