使用Loop下载许多文件时,Nodejs丢失了数据 [英] Nodejs lost data when using Loop for download many files

查看:155
本文介绍了使用Loop下载许多文件时,Nodejs丢失了数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我尝试从我的服务器下载许多文件

Today, I'm try to download many file from my server

download.js

download.js

function getPhotos(req, res) {
  //Get User Photos
  var fileReader = fs.readFile('./../data/user.json', 'utf8', function(err, data) {
    if (err)
      console.log(err);
    else {
      var dataJson = JSON.parse(data);
      //console.log(dataJson.Person);
      for (var i = 0; i < dataJson.Person.length; i++) {
        var options = {
          host : '10.16.47.128', // Local Server IPAddress
          port : 2013, //Port
          path : '/ExternalServer/avatar/' + dataJson.Person[i].Username + '.jpg',
        };
        //console.log(dataJson.Person[i].Username);
        var fileAvatarPhotos = fs.createWriteStream('./../avatar/' + dataJson.Person[i].Username + '.jpg');
        fs.exists(fileAvatarPhotos, function(exists) {//Check Exist File
          if (exists) {
            var req = http.get(options, function(res) {
              //console.log(res);
              res.pipe(fileAvatarPhotos);
            });
          } else {
            var req = http.get(options, function(res) {
              fs.writeFile(fileAvatarPhotos, '', function(err) {
                if (err)
                  return console.log(err);
                var req = http.get(options, function(res) {
                  //console.log(res);
                  res.pipe(fileAvatarPhotos);
                });
              });
            });
          }
        });
      }
    }
  });
  //End Get User Photos
}

当我运行代码时:

node download.js

系统下载了所有照片,但照片大小为0kb。
并有错误:

System downloaded all photos, but size of photo is 0kb. and has error:


stream.js:81
throw er; //管道中未处理的流错误。
^错误:好的,关闭

stream.js:81 throw er; // Unhandled stream error in pipe. ^ Error: OK, close

此外,当我抛出循环 时(EX:修复dataJson) .Person [i] .Username,将i更改为10)

Beside, when I Throw Loop (EX: fix dataJson.Person[i].Username, change i to 10)

运行代码后,系统重新生成正确的照片。

after running code, system reurn correct photo.

发生了什么事?
如何解决?

what are happening? How to fix it?

最好的考虑。!

推荐答案

在for循环中实现这样的功能并不是一个好主意。那是因为在循环内部你有异步操作,但是循环的每次迭代都会立即执行。在您的情况下,您正在定义 fileAvatarPhotos ,这是一个WriteStream对象。 http.get 方法是异步的,因此在其回调中,您可以使用为循环的第三次迭代定义的 fileAvatarPhotos ,或者第四次或第一次迭代。这取决于。您可以尝试将代码转换为以下内容:

It's not a good idea to implement such a functionality in a for loop. That's because inside the loop you have async operations, but every iteration of the loop is executed immediately. In your case you are defining fileAvatarPhotos which is a WriteStream object. The http.get method is asynchronous, so in its callback you may use the fileAvatarPhotos defined for the third iteration of the loop, or the fourth or maybe the first one. It depends. You may try to convert your code to something like this:

var files = ["file1.jpg", "file2.jpg", "file3.jpg"];
var readFile = function(callback) {
    if(files.length > 0) {
        var file = files.shift();
        http.get({path: file}, function(res) {
            // ... process the result
            readFile(callback);
        })
    } else {
        callback();
    }
}
readFile(function() {
    console.log("reading finishes");
});

I.e。 readFile 一次又一次地被调用,直到 files 数组中没有其他元素。

I.e. readFile is called again and again till there is no more elements in files array.

这篇关于使用Loop下载许多文件时,Nodejs丢失了数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆