How can I set a variable equal to the data that fs.createReadStream returns, so I can print out that variable and then I'll get the file contents.
var rs = fs.createReadStream('file.txt', {encoding: 'utf8'});
How can I get the returned data into a variable for later usage.
Thanks
If you just want all of the data at once in a variable, you should use fs.readFile instead.
fs.readFile('file.txt', {encoding: 'utf8'}, function(err, data){
// Use the 'data' string here.
});
That said, depending on your use-case, it would be better to leave the data as a stream and process it in chunks as it is loaded.
This code will help:
var data = [];
var fs = Meteor.npmRequire('fs');
CSV().from.stream(
fs.createReadStream(file),
{'escape': '\\'})
.on('error', function (err) {
console.log(err);
})
.on('record', Meteor.bindEnvironment(function (row, index) {
data.push({
ESSID: row[0],
position: {
lat: row[4],
lng: row[5]
},
publicposition: {
lat: row[11],
lng: row[12]
},
publichost: row[13]
})
}), function (err) {
console.log(err);
})
.on('end', Meteor.bindEnvironment(function (row, index) {
//do something with 'data'
}))