Why the need for Meteor.method?

Can I not simply define a function and call it the normal JavaScript way? When do I need to specify methods to be either defined or called within Meteor.methods? Related to security right? Because I have Synced job that needs to make an HTTP call every so often, and not sure where to structure it for best practice or efficiency. Thanks.

Because if you declare and call a normal function, that function is running on either client or server side.

A Meteor.method is a way to define a function on the server side, and Meteor.call it from the client side. Meteor.call is not a call like myFunction.call(this, ...arg), it is invoking a method that is defined on the server from the client.

Of course, this method also runs on the client when available, but that is only a simulation in the interest of latency compensation.

Long story short; you need it to be able to invoke server code from the client.

“A Meteor.method is a way to define a function on the server side…” but I thought having all methods inside the folder

/server

is already doing that?

In a way, yes. But look at this:

You need Meteor.method to be able to call a server side method from the client.

Thanks so does mean a method within the server side calling another method also residing within server side then there is no need to defining it within the Meteor.method section? Or for best practice put them all in?

This is how I structure my methods

// i have some function i want to run on the server
export default function changeSomethingAboutFoo() {
  // some mutations
}


// another file
import changeSomethingAboutFoo from 'imports/helpers';

Meteor.methods({
    "examples:changeSomething": function (someParam) {
       return  changeSomethingAboutFoo();
    }
});

This way if the client needs to call it sure, but if i need to use the function in server code then go for it.

1 Like