James T.
James T.

Reputation: 978

What is the meaning of the syntax in the Meteor.method function?

Example from https://www.meteor.com/tutorials/react/security-with-methods

Meteor.methods({
  'tasks.insert'(text) {
    check(text, String);

    // Make sure the user is logged in before inserting a task
    if (! this.userId) {
      throw new Meteor.Error('not-authorized');
    }
}

It looks like the only argument it takes is of type Object, built-in in JS. But, the confusing part is that it is using strings to define the names of the functions within that object.

Why can't I just

tasksInsert(text) {
  // ...
}

Upvotes: 0

Views: 129

Answers (2)

Daniela Muniz
Daniela Muniz

Reputation: 123

The Metor.methods() defines the server-side methods of your Meteor application. It is not a simple function calling. With that you are defining which functions are going to be called by the client.

This is a way to your templates interact your dataBase, check, validate and make changes.

You can only call: Meteor.call('stringName_ofFunction', {params}, (err, result)=>{CallBack}); if you previously defined this 'stringName_ofFunction' as a function inside a Meteor.methods();.

The thing is, as your application gets bigger you can define different files for your serverSide methods, so you can get more organized. You are going to use Meteor.Methods(); to tell Meteor that those functions inside that method are your server side functions to be called by Meteor.call() on the client-side.

Upvotes: 0

James T.
James T.

Reputation: 978

Using a string to define the name of a method within the scope of a class or a JS Object is acceptable.

j@j-desktop:~$ node
> function 'add'(x, y) {
function 'add'(x, y) {
         ^^^^^

SyntaxError: Unexpected string

> 'add'(x, y) {
... function 'add'(x, y) {
         ^^^^^

SyntaxError: Unexpected string

> 'add'(x, y) {
... return x + y;
... }
... 
... ;
... 
> class A {
... 'add'(x, y) {
..... return x + y;
..... }
... 
... }
undefined
> let a = new A();
undefined
> a.add(1, 1);
2
> let obj = {
... 'addTwo'(x) {
..... return x + 2;
..... }
... }
undefined
> obj.addTwo(4);
6

Upvotes: 2

Related Questions