1

I want to write a Method to update a document. The client calling the Method may not want to update all fields exposed by the Method via its parameters. What is the right way to implement this in Meteor?

k c
  • 211
  • 1
  • 2
  • 15

1 Answers1

1

You can add some optional arguments to a javascript method:

function updateDocument(requiredArg, optionalArg){
  // optionally set the contents of your optional argument to a default
  optionalArg = (typeof optionalArg === 'undefined') ? 'default' : optionalArg;

  //update your document

}

An other way of passing (multiple) arguments is to put them in an object:

var myDocument = Documents.find({foo: 'bar'})
var myUpdatedFields = {
  field1: 'test',
  field5: 'option',
  field6: 'etc. etc.'
}

updateDocument(myDocument._id, myUpdatedFields);

This calls the updateDocument method with a required argument (the document id) and a set of fields that needs to be updated.

tuvokki
  • 720
  • 1
  • 10
  • 18