Creating a node.js to auto-download a zip file, containing one text file with a list of 5000 UserID's and Usernames (CSV). An example text file would be:
12345,JSmith
728422,YPatel
Every time I run this, I get different results. I think the likely cause is I am attempting to parse the unzipped file, before unzip has finished saving to disk.
Is there a way to either asynchronously parse the unzipped data, or at least wait until unzip is complete, before trying to parse the extracted file? (I am also happy to do this whole process from memory, and avoid disk)
I am very new to node.js.
function downloadAndUnzip(URL, doWhenFinished){
require('request')
.get(URL)
.on('error', function(error) {
console.log(error)
})
.on('end', doWhenFinished)
.pipe(require('unzip').Extract({path:'./'}))
}
var parseFile = function(){
var fs = require('fs')
var array =
fs.readFileSync('usernames.txt').toString().split("\n\r");
for (i in array) {
user = array[i].split(",");
console.log("UserID: " + user[0] + " Username: " + user[1]);
}
}
}
DownloadAndUnzip(URL, parseFile())