Created
April 15, 2014 09:02
-
-
Save cristiandley/10715735 to your computer and use it in GitHub Desktop.
READ A CSV ON METEOR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
| | |
| READ CALL .html | |
| | |
*/ | |
<template name="csvRead"> | |
<input type="button" value="Click" /> | |
</template> | |
/* | |
| | |
| MANAGER .js | |
| | |
*/ | |
Template.csvRead.events({ | |
'click input': function () { | |
Meteor.call("readcsv"); | |
} | |
}); | |
/* | |
| | |
| METHOD | |
| | |
*/ | |
Meteor.methods({ | |
readcsv: function(){ | |
var fs = Npm.require('fs'); | |
var path = Npm.require('path'); | |
var basepath = path.resolve('.').split('.meteor')[0]; | |
var yourcsv = '/yourcsv.csv'; | |
CSV().from.stream( | |
fs.createReadStream(basepath+yourcsv), | |
{'escape': '\\'}) | |
.on('record', Meteor.bindEnvironment(function(row, index) { | |
console.log(row[0]); | |
console.log(row[1]); | |
console.log(row[2]); | |
//you could continue with your rows :D | |
//... | |
//or implement your insert to a mongo collection | |
}, function(error) { | |
console.log('Error in bindEnvironment:', error); | |
} | |
)) | |
.on('error', function(err) { | |
console.log('Error reading CSV:', err); | |
}) | |
.on('end', function(count) { | |
console.log(count, 'records read'); | |
}); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment