In meteor I can read a file like this:
myjson = JSON.parse(Assets.getText("lib/myfile.json"))
Now i want to iterate through a folder, and read all the available json files. What would be the best way to do this without installing extra NPM packages. Thank you for your time.
I'm not sure if this is the best way, but is certainly an easy one:
var fs = Npm.require('fs');
fs.readdir('./assets/app/myFolder', function(e, r) {
_.each(r, function(filename) {
Assets.getText('myFolder/' + filename);
});
});
I wrapped Hubert OGs code into a function with Meteor.bindEnvironment. I believe this is necessary because of fibre not available outside of the Meteor environement. see https://www.eventedmind.com/feed/49CkbYeyKoa7MyH5R
Beware that external Node packages have different document root than Meteor.
var done, files;
var fs = Npm.require('fs');
files = fs.readdirSync("../../../../../server/collections/lib/", function(e, r) {});
done = Meteor.bindEnvironment(function(files) {
return _.each(files, function(filename) {
var myjson;
myjson = JSON.parse(Assets.getText("lib/" + filename));
/* do Something */
});
}, function(e) {
throw e;
});
done(files);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With