How to create Meteor.settings.public within my app?


#1

I created a file /lib/startup.js containing :

Meteor.startup(function() {

  console.log("/lib/startup.js ~~~~~ Starting to configure. ~~~~~ ");
  if (Meteor.settings.public                          == undefined)
      Meteor.settings.public                          = {};

  if (Meteor.settings.public.NAME                     == undefined)
      Meteor.settings.public.NAME                     = "Whoopie Cushion";

  console.log("/lib/startup.js ~~~~~ Check name ? : " + Meteor.settings.public.NAME);

});

But it only works on the server, which is to say, of course, that it doesn’t work at all.

Server log :

/lib/startup.js ~~~~~ Starting to configure. ~~~~~ 
/lib/startup.js ~~~~~ Check name ? : Whoopie Cushion

Browser log :

/lib/startup.js ~~~~~ Starting to configure. ~~~~~ 
Uncaught TypeError: Cannot read property 'public' of undefined

Is the command line switch --settings <filename> the only way to set up public settings?


#2

I have just setup an alias for running in dev.

alias m="meteor --settings=settings.json"

edit: I am assuming you are just annoyed at having to add that argument when running meteor like I was before I made the alias… I haven’t actually looked into checking / changing the settings on startup.


#3

I have a problem with the way it is – new users who don’t want to read documentation (eg, everyone) !

I need a default value for a public setting that gets set correctly, even if my user’s don’t prepare and use the settings.json file, let alone go the extra step of setting up an alias for the settings file.


#4

If there are no public settings because the app wasn’t started with --settings then the client doesn’t even have Meteor.settings, because the client only gets the public part of the settings.

If you insist you could make this work by adding the following to your code in the beginning of your startup function

if(Meteor.isClient && Meteor.settings === undefined) Meteor.settings = {};

#5

So obvious! :unamused:

I thought there was some special magic to Meteor.settings, but apparently it’s just another Javascript collection construct,

Thanks. It worked.


#6

Today I worked on a new package for configuration loading (4commerce:environments). I came into same trouble and found the problem in a meteor package file (server_environments.js).

I placed a PR on GitHub to solve that in future: https://github.com/meteor/meteor/pull/4704

By now you could handle it same as I did : https://github.com/4commerce-technologies-AG/meteor-package-environments/blob/master/environments.js#L132-LL143

Currently you have to initialize __meteor_runtime_config__.PUBLIC_SETTINGS if it was not set during boot.js.

Cheers
Tom