Nathan
Nathan

Reputation: 470

Meteor.method arguments used in Mongo call

I want to write this call as a Method: Posts.update(this._id, { $set: { status: 'accepted' }})

Meteor.methods({
    ...
    updatePost: function(id, key, value) {
        Posts.update(id, { $set: { key: value }});
}
})

Meteor.call('updatePost', this._id, 'status', 'accepted') does not work. I'm guessing because of the 'status'? How could I get this to work? Thanks.

Upvotes: 0

Views: 67

Answers (2)

Tarang
Tarang

Reputation: 75945

Variables can't be defined as keys in javascript. Something like this could do it though:

var update = {};
update[key] = value;
Posts.update(id, { $set: update });

Upvotes: 1

David Weldon
David Weldon

Reputation: 64312

I suspect it isn't working because you can't use variable names as keys in JavaScript object literals. You'll need to use bracket notation instead. Give this a try:

Meteor.methods({
  updatePost: function(id, key, value) {
    check(id, String);
    check(key, String);
    check(value, String);

    var obj = {};
    obj[key] = value;
    return Posts.update(id, {$set: obj});
  }
});

Upvotes: 2

Related Questions