I’m trying to figure out if what I’d like to do is possible, or if it will require dynamic imports.
I have a lot of code inside a module that just performs “data extraction”. Meaning it just queries mongo and loads it into an object for consumption by other modules. The twist is that of course I’ll need an ID to query on and the ID is only known once the client makes a server side call to a Meteor Method.
For example:
// imports/server/data-extraction.js
import Test from '../collections/test';
import { THE_USER } from '../server/globals';
let query = { _personId: THE_USER };
let {
FirstName,
MiddleName,
LastName,
} = Test.findOne(query);
Docs = {};
Docs.Test = {
FirstName,
MiddleName,
LastName,
}
export default Docs;
So when the application starts, the module is loaded, and there’s no data. It’s only after the Meteor Method is called that THE_USER is known and I can query for it in this module. Yet, by this time it’s too late, the module is already loaded and never gets loaded again. So the way I “fixed” this is to wrap this all in a function that is called “on-demaind”.
// imports/server/data-extraction.js
import Test from '../collections/test';
import { THE_USER } from '../server/globals';
const CallMeWhenYouHaveTheData = () => {
let query = { _personId: THE_USER };
let {
FirstName,
MiddleName,
LastName,
} = Test.findOne(query);
Docs = {};
Docs.Test = {
FirstName,
MiddleName,
LastName,
}
return Docs;
}
export { CallMeWhenYouHaveTheData };
Now in another module that imports the above module, I can simply call the method when the ID is known and all works out okay. But the thing is, I’d like to not have to wrap this code in a function, and only have the module loaded/called when I have the ID from the Meteor Method. Is what I’m looking for Dynamic Imports or is there another way?