Вопрос

I've been working through a few others, also this of looping through array to http.get data from a variety of sources. I understand that nodeJS is working asynchronously which is allowing the files to be written empty or with incomplete data, but I can't seem to get past this point.

Problem: calls are made, files are built but the files are always empty

Goal: loop through an array to create files locally from the sites data. Here is what I've got so far:

var file_url = 'http://js.arcgis.com/3.8amd/js/esri/',
    DOWNLOAD_DIR = './esri/',
    esriAMD = [ '_coremap.js', 'arcgis/csv.js'];

function readFile(callback) {
  if (esriAMD.length > 0) {
    var setFile = esriAMD.shift(),
        file_name = url.parse(file_url).pathname.split('/').pop(),
        trial = setFile.split('/').pop(),
        file = fs.createWriteStream(DOWNLOAD_DIR + trial);

    http.get(file_url + esriAMD, function(res) {
      res.on('data', function(data) {
        file.write(data);
        console.log(setFile + ' has been written successfully');
      });
      res.on('end', function(){
        console.log(setFile + ' written, moving on');
        console.log(esriAMD.length);
        readFile(callback);
      });
      //readFile(callback);
    });
  } else {
    callback();
  }
}
readFile(function() {
  console.log("reading finishes");
});

Any insight would really help.

thanks,

Это было полезно?

Решение 2

Solution: I was passing the wrong variable into the http.get

Working code:

var file_url = 'http://.....',
DOWNLOAD_DIR = './location/';
esriAMD = ['one', 'two', 'three'..0;

function readFile(callback) {
if(esriAMD.length > 0) {
    var setFile = esriAMD.shift(),
    file_name = url.parse(setFile).pathname.split('/').pop(),
    trial = setFile.split('/').pop(),
        file = fs.createWriteStream(DOWNLOAD_DIR + trial);

    http.get(file_url + setFile, function(res){
        res.on('error', function(err){
            console.log(err);
        });
        res.on('data', function(data){
            file.write(data);
            console.log(setFile + ' started');
        });
        res.on('end', function(){ 
            console.log(setFile + ' completed, moving on');
        });
    });
} else {
    callback();
 }
}

Другие советы

var esriAMD = [....];
...
function readFile(callback) {
    ...
    http.get(file_url + esriAMD, function(res) {
        ...

concatenating strings with arrays may yield unexpected results.

you want to make sure that

  • you know what URLs your program is accessing
  • your program deals with error situations (where the fsck is res.on('error', ...)?)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top