[SOLVED] How do I call a meteor method on the server side?

Hi,

How do I call a meteor method on the server side?
I am using mdg:validated-method to defined my meteor methods.

How do I call a validated meteor method on the server side?

Thanks,

Scroll down the page and you’ll see it talking about methods on the client and on the server.

You call them exactly the same way as you do on the client.

However, it’s good practice to only use methods for client->server requests, and extract the shared part to another function or module that can be called directly on the server
Meteor Tuts explains better than me:

3 Likes

Meteor.call is not a good practice ?

It’s good for use on the client, but not best practice on the server.

Think of a traditional REST app, would you get the server to make HTTP requests to itself in order to call a method? Or would you just import and call the function?

1 Like

Yes, my question is how to bypass the client call and directly execute the server code?

Define function:

export function doTheThing(args) {
  // ...
}

Call function:

import { doTheThing } from '/imports/api/thingDoer';

doTheThing(/* some args */);

Use function in method:

import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { doTheThing } from '/imports/api/thingDoer';

export const doThingMethod = new ValidatedMethod({
    name: 'do.the.thing',
    validate(args) {
        new SimpleSchema({ /* some schema */ }).validate(args);
    },
    run(args) {
        return doTheThing(args);
    },
});
3 Likes

I had the same troubles when I started. Thanks @coagmano for the great explanation. I think lot of beginners in Meteor have a hard time to understand that on the server side it’s just a simple app and we can do the same things we would do with a simple express app. Maybe we should consider improving the documentation about this as well to demistify the “magic”.

1 Like

True. When I tried Meteor for the first time I fell in love with it. I thought you could prototype an app really fast with it which I still think it is true. Also I don’t see the point from moving the meteor prototype to another framework.

The only thing is that Meteor beginners commit many common pitfalls because Meteor is unique and its concepts. Docs should be improved. Luckily the forum helps but docs should be improved.

Also I don’t see the point of using meteor with angular, react or vue. I like Blaze.
I use angular and react at work but for my personal side project I chose Meteor.

1 Like

Yes, I think we should all play a role. I will see how I can bring my part to the documentation in link with the new roadmap.

Also I don’t see the point of using meteor with angular, react or vue. I like Blaze.
I use angular and react at work but for my personal side project I chose Meteor.

Agree to disagree on this part. I am a big React fan and I would never go back. Yes Blaze is great. But React has so much library, ready-made component etc… and is in continuous improvement. Also using popular library like React makes it easier for people to join the meteor community. They already know React they just have to start the meteor app and they have a ready-to-use backend and database that they can combine with their React knowledge. I think it is very imporant.