Hexatonic
Hexatonic

Reputation: 2397

How do you expose CSV data to the client in Meteor?

I'm just learning Meteor, and I'm fiddling with something very simple to understand how the framework works. I load a CSV file with country names / country ids that I want to use in a SelectBox on the client.

I installed baby-parse

meteor add harrison:babyparse

I then use

Meteor.startup(function() {
  parsed = Baby.parse(Assets.getText('geo.csv'));
  Countries = parsed.data;
});

How can I expose the Countries data to the client?

Upvotes: 0

Views: 82

Answers (1)

kartikshastri
kartikshastri

Reputation: 170

Seems like you'd be better off populating a Mongo collection with the country data.

If you really want to just access a server method from the client, you can call a meteor method on startup from the client and set the result of that to a session variable. For example:

if(Meteor.isClient){
  Meteor.call('getCountryData', function(err, res){
    if (res){
      Session.set('CountryData', res)
    }
  });
}

if (Meteor.isServer){
  Meteor.methods({
    'getCountryData':function(){
      parsed = Baby.parse(Assets.getText('geo.csv'));
      Countries = parsed.data;
      return Countries;
    }
  });
}

Then use Session.get("CountryData") wherever needed on the client.

Again, highly recommend populating a collection in your database and then publishing/subscribing data to the client instead of this approach. Here's a good primer on the basics of MongoDB w/ Meteor: http://meteortips.com/first-meteor-tutorial/databases-part-1/. Chapter 11 (Publish & Subscribe) is what you want to look at after understanding the basics.

Upvotes: 3

Related Questions