문제

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