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?
Asked
Active
Viewed 681 times
1
-
4why would that be any different from optional parameters in any other javascript function? – Christian Fritz May 18 '16 at 15:46
-
Thanks. That answered my question. – k c May 19 '16 at 11:42
-
Possible duplicate of [Set a default parameter value for a JavaScript function](http://stackoverflow.com/questions/894860/set-a-default-parameter-value-for-a-javascript-function) – Christian Fritz May 19 '16 at 16:18
1 Answers
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