Hello,
I would like to try the package mdg:validatedMethods but I do not manage to make it work.
From the documentation:
//Method definition
const method = new ValidatedMethod({
name, // DDP method name
mixins, // Method extensions
validate, // argument validation
run // Method body
});
And here the example:
// This is the body of the method. Use ES2015 object destructuring to get
// the keyword arguments
run({ listId }) {
// `this` is the same method invocation object you normally get inside
// Meteor.methods
if (!this.userId) {
// Throw errors with a specific error code
throw new Meteor.Error('Lists.methods.makePrivate.notLoggedIn',
'Must be logged in to make private lists.');
}
What does the run method do and what does it expect in argument ? Why is the argument braced {} ? What am I supposed to pass in run ?
So basically what this package is trying to do is encode some best practices to make them easier to use. A normal vanilla meteor method runs on the client and the server in parallel (assuming it’s defined on both the client and the server). This package splits that method logic up into a validate and a run part. First the validate runs on the client and if that passes, the run part runs on the client and the server (again assuming it’s defined on both sides). This saves you from wasting server resources when you could have detected the validation fail on the client.
The second part that’s confusing you is just the new ES2015 object destructuring syntax (read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment). But it’s just syntactic sugar and you could write the same method this way:
run(object) { var listId = object && object.listId; //
thisis the same method invocation object you normally get inside // Meteor.methods if (!this.userId) { // Throw errors with a specific error code throw new Meteor.Error('Lists.methods.makePrivate.notLoggedIn', 'Must be logged in to make private lists.'); }
This method takes an object as a parameter.
1 Like