Meteor.js - Production Meteor.settings throws TypeError undefined

1.9k views Asked by At

I am new to Meteor, and am setting up dev, stage and production Meteor.settings.

Everything works fine on my local system using the "development" settings, but when I bundle it, and run on Digital Ocean, it breaks. If I removed the call to the settings file, and replace it with the text string, everything works fine.

Here is the settings file, named "1_settings.js" to load before all other js files (in theory at least?):

console.log("=> inside 1_settings.js"); 

environment = process.env.NODE_ENV || "development";

var settings = {
  development: {
    public: {},
    private: {

      "firstUser": {
        "id": "ZsKaw3kSbQ4uSvzJg"
      },

    }
  },
  staging: {
    public: {},
    private: {}
  },
  production: {
    public: {},
    private: {

      "firstUser": {
        "id": "oPMfpAuwQ3nuTdpTA"
      },

    }
  }
};

if (!process.env.METEOR_SETTINGS) {
  console.log("=> No METEOR_SETTINGS passed in, using locally defined settings.");

  if (environment === "production") {
    Meteor.settings = settings.production;
  } else if (environment === "staging") {
    Meteor.settings = settings.staging;
  } else {
    Meteor.settings = settings.development;
  }

  // Push a subset of settings to the client.
  if (Meteor.settings && Meteor.settings.public) {
    __meteor_runtime_config__.PUBLIC_SETTINGS = Meteor.settings.public;
  }
}

Here is where I use the settings:

if(Meteor.isServer) {

var settings = Meteor.settings.private;

console.log("=> settings: " + JSON.stringify(settings));

Meteor.startup(function () {
    // bootstrap the admin user if they exist -- You'll be replacing the id later
    if (Meteor.users.findOne(settings["firstUser"]["id"]))
        Roles.addUsersToRoles(settings["firstUser"]["id"], ['admin']);

As you can see, it's breaking on the second "if" statement of the above code, where I'm calling settings["firstUser"]["id"], as shown in the error below:

TypeError: Cannot read property 'firstUser' of undefined
    at app/server/app.startup.js:8:42
    at /home/qdev/bundle/programs/server/boot.js:175:5

/home/qdev/bundle/programs/server/node_modules/fibers/future.js:173
                                            throw(ex);
                                                  ^

Obviously, I understand that the settings property is undefind, or null. What I don't understand is why? Even if I didn't set the $NODE_ENV=production (which I have), it should still fall back to the development settings. Help is appreciated!

1

There are 1 answers

0
Marco de Jongh On BEST ANSWER

You are using Meteor.settings wrong. It supposed to be filled by meteor itself with runtimesttings you specify.

To do this you should create a JSON file containing your setting then use the --settings #pathtofile# parameter when calling meteor for development. Make sure it's valid JSON!

This will properly fill Meteor.settings and also makes you able to change settings later.

On the server you use a env variable to point to the settings file. Eventedmind made a video about how to set this up: link