× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Reading JSON file and creating database entries in meteor.

Feb 13, 2015
<pre><code class="language-javascript"> var citiesCount = Cities.find().count(); // if we already have entries in the db, don't insert again. if (citiesCount &gt; 0) return; // code to run on server at startup Assets.getText('cities.txt', function(err, data) { var content = EJSON.parse(data); for(country in content){ console.log('inserting', country); Cities.insert({country: country, cities: content[country]}); } }); // the cities.txt file must be insite a directory named "private" in your root directory of the app // you have to run it in the server portion of the code in meteor.</code></pre> <p>In this session we tried to read a json file and parse the entries into a db collection in meteor.js. </p> <p>Above is an example code that can do the trick. </p> <p>The file in context can be found here - https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json</p> <p>copy the contenst of the file and paste it into a cities.txt file and put the file in a directory named "private" in the root of your app.</p>
comments powered by Disqus