Reputation: 63619
How can CSV files be read into a Meteor app from a filesystem path in the /private
directory?
Found fast-csv
packaged for Meteor but how can createReadStream
be created for feeding into the fast-csv
package.
Upvotes: 0
Views: 1936
Reputation: 64312
Using Oskar's suggestion of papa-parse, you can do something like this:
$ meteor add harrison:papa-parse
Then on your server:
// read your file as a csv string (assuming it's in the private dir)
var csv = Assets.getText('path/to/your.csv');
// convert the csv to an array of arrays
var rows = Papa.parse(csv).data;
// show the first row
console.log(rows[0]);
Recommended reading:
Assets
API.Additionally, if you are storing the data ahead of time in your private
directory, I'd recommend converting it to a format which can be read without parsing it (i.e. use JSON instead of CSV).
Upvotes: 2
Reputation: 81
I'd use PapaParse to read the CSV (meteor add harrison:papa-parse
), available here . It's super easy to use.
You simply have to use
Papa.parse("http://example.com/file.csv", {
download: true,
complete: function(results) {
console.log(results);
}
});
Or, if you prefer a step-by-step approach you can use this:
Papa.parse("http://example.com/big.csv", {
download: true,
step: function(row) {
console.log("Row:", row.data);
},
complete: function() {
console.log("All done!");
}
});
IMHO it's a very good package, I've used it extensively. More docs [here]. Enjoy!2
Upvotes: 1