سؤال

I have a local json file in my client folder that contains information for <option>s in a <select> tag.

I tried using ajax to fill up <option>s but my app keeps crashing.

What is the proper way to get information out of a local json file in meteor?

هل كانت مفيدة؟

المحلول

To get information from the server you need to use a Meteor method.

To read local file you need to use assets.

 

For example, assuming your file is /private/options.json:

server side

Meteor.methods({
  getOptions: function() {
    return Assets.getText('options.json');
  },
});

client side

var loadOptions = function() {
  Meteor.call('getOptions', function(error, result){
    fillOptions(JSON.parse(result));
  });
};

نصائح أخرى

I had the same problem, I needed to load labels for the client.

Instead of calling the server, the client can directly perform a HTTP call and retrieve the file.

  • Put your file into the public directory
  • Use the HTTP API to get your file

HTTP.get('/yourFile.json', {}, function(error, result) { var parsedFile = JSON.parse(result.content); });

If you are using Iron router be sure to wait for the result before displaying your page with a waitOn.

As the call is asynchronous, it might take some time to get your result.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top