henk
henk

Reputation: 2848

How can I retrive data from my settings collection before anything else is loaded?

I have a mongo collection called Settings. It stores some settings like API keys. Those settings should be adjustable within the application, without accessing the server.

Those settings need to be loaded and be present on the client before anything else. Because in case of the API-keys, the API will crash if the key is not defined before the API starts to load.

I can not use settings.json, because as far as I know, you can not change them during the runtime. Settings.json can only be hardcoded on the server?

Upvotes: 1

Views: 53

Answers (2)

henk
henk

Reputation: 2848

There was a concept misunderstanding here. The easy solution is to check if the api keys are loaded and then run the dependent code. In that way, the app will not crash.

Upvotes: 0

Shrabanee
Shrabanee

Reputation: 2776

Use callback to make your code synchronous.

Try something as below, which may solve your problem:-

Meteor.methods({
'getApiKey': function () {
    return Settings.findOne().key //there is only one entry so far
 } });

//
function setKey(callback)
{
   Meteor.call('getApiKey', function (err, result) {
   if (err) console.log(err);

      Session.set('key', result);
      callback(err);
   });
}

//Wait for the `getApiKey` to complete, use callback.
setKey(function(err)
{
     var apiKey = Session.get('key'); //Set the key in the callback function
})

EDIT:-

Try this instead:-

Meteor.methods({
'getApiKey': function () {
    return Settings.findOne().key //there is only one entry so far
 } });

  var apiKey;
   Meteor.call('getApiKey', function (err, result) {
   if (err) console.log(err);

      Session.set('key', result);
      apiKey = Session.get('key'); 
      console.log(apiKey);
   });

Let me know if it works.

Upvotes: 1

Related Questions