Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use environment variable in client and server at the very beginning

Tags:

meteor

I'm using environment variable when I start my meteor project like this

MYVAR1="foo bar sdf" MYVAR2=0 meteor

then in a file [root]/lib/constants.js I have

ENV = ["sjobs", "unisight", "dfgdfsgf"];
AUTHENTICATION = true;

if (Meteor.isClient) {
    Meteor.call("getGlobals", function (error, result) {
        "use strict";

        if (error === undefined) {
            AUTHENTICATION = result.AUTHENTICATION;
            ENV= result.ENV;
            console.log(result);
        } else {
            console.error(error);
        }

    });
}

if (Meteor.isServer) {
    var univasENV = ["urb", "unisight", "sjobs", "unicloud"];
    var tmpenv;


    if (process.env.MYVAR2 !== undefined && parseInt(process.env.MYVAR2, 10) === 1) {
        AUTHENTICATION = false;

    }

    if (process.env.MYVAR1 !== undefined) {
        tmpenv = process.env.MYVAR1.split(" ");
        ENV = [];
                _.each(tmpenv, function (value) {
            "use strict";

            if (univasENV.indexOf(value) !== -1) {
                ENV.push(value);

            }
        });
    }
}

in another file [root]/server/methods.js I have:

Meteor.methods({
   getGlobals: function(){
        "use strict";
        console.log(AUTHENTICATION, ENV);
        return {
            auth: AUTHENTICATION,
            env: ENV
        };
    }
});

the server part works as I expect, however the code in the client side is executed after everything has been render or loaded. The problem in here is Meteor.call() that runs async and I cannot use (as far as I know) Meteor.wrapAsync().

I also tried to write those value that I need in a collection and then read it from the client (all this in [root]/lib/constants.js) but it behaves exactly as method/call.

so the question is how can I pass some values from the server to the client at the very beginning?

like image 455
juanp_1982 Avatar asked Dec 09 '22 03:12

juanp_1982


2 Answers

It seems you would like to use an environment variable, set on the server, in the client side java script. I would consider using Meteor.settings so you can specify your various environment settings in json under the key public, when you start meteor. Which should be available to your client side code.

If the settings object contains a key named public, then Meteor.settings.public will be available on the client as well as the server. All other properties of Meteor.settings are only defined on the server. http://docs.meteor.com/#/full/meteor_settings

Hope that helps

like image 81
jackkav Avatar answered Mar 23 '23 01:03

jackkav


If you're using a router, you could try to load the variables there and only render AFTER the variables are retrieved.

As a clever workaround, you could use a collection to store them as configuration. On startup the server writes all values into the database and then your client can retrieve them (and the configuration variables will be reactive).

When you make the method call from the client to the server, this is executed asynchronously. By the time the response comes back your page is rendered already, which is why you see these issues.

like image 20
Peter Ilfrich Avatar answered Mar 23 '23 00:03

Peter Ilfrich