Read the statement by Michael Teeuw here.
Passing Variables Around
-
I feel like I’m losing my mind. How do I pass a variable from js file to js file here? I’ve tried
module.exports
and the traditionalexports.name = name;
in the file where it’s set, then in the file where I want to use it:var myModule = require('./module'); var name = myModule.name;
But when I go to use the variable, my code editor shows me it IS available, but it blows up when I start the mirror (I get the ole “Your config is broken” message). In fact, if I add anything other than the config variable to config.js, it blows up. It seems like the export is working, but there’s something preventing additional variables from being used. What am I missing? What’s the best practice for accomplishing this?
EXAMPLE:
Say I wanted to create a
var name = "Jeremy";
and show"Hi " + name
in the config.js of my compliments module’s config object. Easy, right?BACKGROUND:
I have my mirror connected to a Mongo DB that also serves my personal website. I plan to use my mirror as a display mechanism for data from my site. For example, I might build an editor on my site to create new compliments, save to my db, then the mirror would show them. Or similarly, display some text from my db in the Hello World module, etc. Seems straightforward enough. The db connection is working. Just trying to get the darn variables where I need them.
-
I really don’t understand what you are trying to do! It seems you are trying to put some code inside config.js?!?
-
Yes, @Cato I was afraid I overcomplicated the question. I actually tried to simplify it later, but apparently there’s a time limit on editing. Let me try simplify.
Let’s say I want to take the Hello World module and base it’s text on a variable. That’s the simplest form.
So hypothetically, my config.js could look like:
var someVariable = "myString"; var config = { modules: [ { module: 'helloworld', position: 'top_bar', config: { text: someVariable } } ] }
Unfortunately, using a variable in place of an actual string of text seems to break the config file. Surely there must be a way!
-
Ok, I understand what you are trying to do. But I’m pretty sure it’s not possible to insert variables into the config file.
If you can’t access your MongoDB using a client side API, I would suggest adding a node_helper to your module and sending “notifications” between frontend (module) and backend (node_helper) asking for the information you need? You can do this in the start-part of the module if it is an one time thing.
If you haven’t, make sure to read and understand this documentation: https://github.com/MichMich/MagicMirror/tree/develop/modules
-
Hello,
I was trying to clean my config file and the following is working for me.
Let me know if it’s working for you.
/* Magic Mirror Config Sample
*- By Michael Teeuw http://michaelteeuw.nl
- MIT Licensed.
*/
var stocks_list = “INDEXSTOXX:SX5E”;
var weather_api_key = “XXXXXXXX”;
var weather_pws = “XXXXXXXX”;
var hue_bridge_ip = “XXXXXXXX”;
var hue_user_id = “XXXXXXXX”;
var voice_code = “JARVIS”;var alert = { module: ‘alert’ };
var stocks = { module: ‘mmm-stocks’, position: ‘top_bar’, config: { stocks: stocks_list, updateInterval: 37000 } };
var clock = { module: ‘clock’, position: ‘top_left’ };
var weather = { module: ‘MMM-WunderGround’, position: ‘top_left’, config: { apikey: weather_api_key , weather_pws ,hourly: ‘1’, alerttruncatestring: “french:” } };var hue = { module: ‘MMM-Hue’, position: ‘top_right’, config: { bridgeip: hue_bridge_ip , userid: hue_user_id ,colour: false} };
var hide = { module: ‘mm-hide-all’, position: ‘bottom_right’ };
var voice = { module: ‘MMM-voice’, position: ‘bottom_bar’, config: { keyword: voice_code, microphone: 1, debug: true } };
var config = {
port: 8080,
language: ‘en’,
timeFormat: 24,
units: ‘metric’,
modules: [ clock, hue, stocks, voice, weather ]
};/*************** DO NOT EDIT THE LINE BELOW ***************/
if (typeof module !== ‘undefined’) {module.exports = config;} -
Is this what you really want:
var myName = "Cato"; var myModule = { module: 'MyModule', position: 'top_left', config: { name: myName }}; console.log(myModule);
Output:
{ module: 'MyModule', position: 'top_left', config: { name: 'Cato' } }
-
Basically yes, that’s what I’m trying to do. Would that work, assuming I set it up as a new module according to the guidelines you sent earlier? (thank you by the way, that helped a lot).
Essentially I’m just trying to be able to pull data from my MongoDB (I will probably roll this into its own module – right now I’ve committed the sin of modifying app.js directly until I get my bearings). Then use the Mongo data to populate data in other modules (think Hello World, Compliments, etc.).
It sounds like I will need to create a second module that essentially takes my Mongo data and sends it in the form of a JSON object variable to other modules using sendNotification() and then have them update the DOM to reflect the change. I know there are some nuances, but for the sake of brevity, am I generalizing/thinking about it correctly?