8

I am trying to read a JSON file with Meteor. I've seen various answers on stackoverflow but cannot seem to get them to work. I have tried this one which basically says:

  1. Create a file called private/test.json with the following contents:
[{"id":1,"text":"foo"},{"id":2,"text":"bar"}]
  1. Read the file contents when the server starts (server/start.js):
Meteor.startup(function() {
 console.log(JSON.parse(Assets.getText('test.json')));
});

However this seemingly very simple example does not log anything to the console. If I trye to store it in a variable instead on console.logging it and then displaying it client side I get

Uncaught ReferenceError: myjson is not defined 

where myjson was the variable I stored it in. I have tried reading the JSON client side

    Template.hello.events({
    'click input': function () {
        myjson = JSON.parse(Assets.getText("myfile.json"));
        console.log("myjson")
  });
}

Which results in:

Uncaught ReferenceError: Assets is not defined 
  1. If have tried all of the options described here: Importing a JSON file in Meteor with more or less the same outcome.

Hope someone can help me out

Community
  • 1
  • 1
Jaspermid
  • 461
  • 3
  • 14

2 Answers2

4

As per the docs, Assets.getText is only available on the server as it's designed to read data in the private directory, to which clients should not have access (thus the name).

If you want to deliver this information to the client, you have two options:

  1. Use Assets.getText exactly as you have done, but inside a method on the server, and call this method from the client to return the results. This seems like the best option to me as you're rationing access to your data via the method, rather than making it completely public.
  2. Put it in the public folder instead and use something like jQuery.getJSON() to read it. This isn't something I've ever done, so I can't provide any further advice, but it looks pretty straightforward.
richsilv
  • 7,993
  • 1
  • 23
  • 29
  • So I've created a meteor method on server/start.js Meteor.methods({ readit: function(){ return JSON.parse(Assets.getText('test.json'));; } and call it clientside Template.hello.events({ 'click input': function () { // template data, if any, is available in 'this' if (typeof console !== 'undefined') console.log("You pressed the button"); console.log(Meteor.call('readit')) alert (Meteor.call('readit')) } }); } }); – Jaspermid Jun 29 '14 at 09:54
  • @Jaspermid keep in mind Meteor call is asynchronous, you need to read data out of the callbacks as it won't return a value directly (`var value = Meteor.call("readit")` would return `undefined`). See async call under : http://docs.meteor.com/#meteor_call – Tarang Jun 29 '14 at 12:22
  • @Akshat Thanks for the feedback I do not quite get the documentation. How would I read out data out of the callbacks? Storing it in a variable and then using it doesn't work. <-beginner :) – Jaspermid Jun 29 '14 at 18:11
3

The server method is OK, just remove the extra semi-colon(;). You need a little more in the client call. The JSON data comes from the callback.

Use this in your click event:

if (typeof console !== 'undefined'){
    console.log("You're calling readit");
    Meteor.call('readit',function(err,response){
        console.log(response);
    });
}

Meteor!

  • @RandelS.Hynes That works thanks! One last question:). What would I need to change to return JSON.parse(Assets.getText('test.json')) to have it read an XML called test.xml? Just changing the Json to xml doesn't work ^^ – Jaspermid Jun 30 '14 at 10:43
  • Assets.getText gets the content of a file as a string. If you format that test JSON file as proper XML it will be read as a string. JSON.parse("string") takes a string formatted as JSON and reads it as JSON. I don't do XML, so I don't know if there is an XML equivalent to JSON.parse(). – Randell S. Hynes Jun 30 '14 at 16:45