Method not found if not defined in top level server/ or imports/startup/server, why?


#1

Hi all,

I’m building a big App with the new lazy-loading approach using imports/.

I’m defining my methods under /imports/api/moduleName/methods.js using the export and mdg:validated-method pckg.

Then I’m importing my method under /imports/ui/logicUnit/xyz.js as following:

import {myMethod} from ‘/imports/api/moduleName/methods.js’;

myMethod.call({}, (err, res) => {
if (err) {
handleError(err.error);
}

doSomethingWithResult(res);
});

However, I’m getting a 404 error in my client console log, that says that my method is not found even when I’m importing well the path to my method as above.

When I import the method in my top directory /server/main.js or in my imports/api/startup/server it works. But it doesn’t make sense with the new imports/ (lazy-loading approach) in a big app because then all my methods needs to be called from my /server/main.js or startup even when I don’t need to load all from the beginning and this would be same as before meteor versions when I defined all my methods in the top directory server/ “eager evaluation or loading”.

Should I have to import ALL my methods in the startup or top level server/ to get it working? I repeat this wouldn’t be a lazy-loading and instead it’s go back to the same way of defining the methods into /server like in <1.3.

Please let me know why my methods are not being found when I’m defining them in the /imports/ui/logicUnit/xyz.js

Thanks in advance,


#2

You have a fundamental misunderstanding of why lazy-loading is helpful. The point of lazy-loading files is to reduce the size of the browser client bundle that the web browser downloads for each user when they visit your site. It’s to make your website load faster in a web browser because the size of the JS file the browser downloads is smaller, because you’re not loading some code until the browser needs it. It doesn’t make sense to try and make your server code’s size smaller, because nobody ever downloads it so why would it matter how big it is?

So yes you should import all of your Meteor Methods on the server. Otherwise the client calls a method with Meteor.call() and the server has never been told how to handle that method.


#3

Thank you so much @efrancis,

Yes, you are right, lazy-loading is for the browser client bundle. That’s clear thanks. I had the idea that just importing the method in the client (imports/ui/…) and by exporting the method in /api/…/methods.js using mdg:validated-method, I didn’t know to import again on server/main.js or startup/server/startup.js.

Definitively in order to allow the client to call my methods, I had to import them in /server/main.js or /imports/api/startup/server. So if I have hundreds of methods I have to import the same hundred of methods on my /server/main.js or in my /imports/api/startup/server/startup.js, right?

Even if I’m importing my methods on the server, and for using several functions and return information to the method.call() on the client, I have had to include if(Meteor.isServer()){…} on my methods to get the client working well. How can I avoid using if(Meteor.isServer()){…} on my methods without affecting client-calls data and simulation optimistic-ui?

Thanks in advance