Passing data from Meteor Methods to Modules

I need to pass an Id around server side meteor methods and import files. The Id originates from the meteor method. What’s the best way?

// meteor method file on the server
/server/methods.js

Meteor.method({
  test: function (id) {
    do_something_server_side(id);
  }
});

// module that’s mainly for declaring, deconstructing, and formatting variables. NOTE: The point of declaring this in a module is that i have a LOT of variables to declare and I need to do it in many different spots. This is just a extremely small example.

/imports/server/data-extraction.js

const query = { userId: THE_USER };

let {
  first,
  second,
  third
} = collection_name.findOne(query);

Here I need the data-extraction variables in the module

/server/do_stuff.js

import { first, second, third } from '../imports/server/data-extraction.js'

const do_something_server_side = id => {
  // do stuff with first second and third variables
}

How do I get that Id from the Meteor method, into my data-extraction module so that I can query mongo and use that data in my do_something_server_side function?

Store your variable in the database.

So from /imports/server/data-extraction.js you can update an entry with your variable. Posts.insert(… name:“my_super_secret_variable”, value:“something I want to pass to some where else”);

And from /server/do_stuff.js you can simply var variable = Posts.findOne({name:“my_super_secret_variable”});

console.log(variable);

Boom.

Wait, so instead of passing a variable around on the server, you’re saying I should write the variable to the DB inside the meteor method, then in the module I should query the database for the Id? How do I know what to query for in the db?

Aha! Close, but no. You want to query for your variable name, and fetch it’s contents!

So again, written clearer.

SAVE: {title:“my_cool_variable”, content:“a_b_c_1_2_3”}

other file, LOAD: var my_cool_variable = Variables.findOne({ title:“my_cool_variable” });
console.log(my_cool_variable.content);

Get it?

Right, so use the Id like settings data. Thanks, I think that’ll do it.

Thank you.


Just curious, is this common, using the db as a global on the server-side when using imports?

You know, with out seeing your project, it’s hard to say.

I can pass data this way easily. But I can also use settings.json to set variables. Not sure if you can write to them though.

1 Like

Follow-up question:

Now in my new Module it makes a call to Mongo in order to get the Id it needs to perform it’s operations. Once it has this Id, it makes several Mongo queries like my original example.

Yet, this Id is not there UNTIL the Meteor method has been called. This causes an issue when my app first starts, as apparently the Module attempts to make these Mongo queries on startup even though the code is not used until the Meteor method is called – STRANGE (I thought Modules run only when imported and used!).

Of course if I don’t have the Id yet, so I get an exception when trying to make simple Mongo calls like the following inside the Module:

const query = { userId: THE_USER }; // On startup THE_USER is undefined 

let {
  first,
  second,
  third
} = collection_name.findOne(query);

Should I wrap all my code in a big “if” statement that checks if the Id exists or is there some special “Modules” way of stopping the Module from running until it actually needs to run (like a function call I guess)? Because even if I use an empty string for the Id as a default, I still get an Type error if the query returns nothing:

let userId = '';

if (THE_USER) userId = THE_USER;
 
 const query = { userId: userId }; // On startup THE_USER is undefined 

 let {
   first: test1,
   second: test2,
   third: test3
 } = collection_name.findOne(query);
}
TypeError: Cannot read property 'first' of undefined

The type error is just a Deconstructing issue. If the return is undefined we cannot assign to the Object. Checking for undefined first and assigning a { } Object works:

let userId = '';
if (THE_USER) userId = THE_USER;
const query = { userId: userId }; // On startup THE_USER is undefined 

 // On startup the findOne returns undefined
let record = collection_name.findOne(query);
if (!record) record = {};

let {
  first: test1,
  second: test2,
  third: test3
} = record;