0

I am downloading images in a loop and when I set my loop to for (var i = 0; i < 100; i++) { I have no problem downloading images. but as soon as I change my i range to go more than 99 (inclusive of 100) so anything like: for (var i = 100; i < 200; i++) { OR for (var i = 0; i < 200; i++) { I get an error. Here is my code:

var fs = require('fs');

var request = require('request');

var download = function(uri, filename, callback){
  request.head(uri, function(err, res, body){
    request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
  });
};

var arrLength = allProducts.length;

for (var i = 0; i < arrLength; i++) { 

    let imgName = allProducts[i].code;    
    let imgUrl = allProducts[i].nofrillsImgLink    

    download(`${imgUrl}`, `./allProductsImg/${imgName}.png`, function(){              
      });
};

And here is the error I am getting:

events.js:287
      throw er; // Unhandled 'error' event
      ^

Error: Invalid URI "undefined"
    at Request.init (.../node_modules/request/request.js:273:31)
    at new Request (.../node_modules/request/request.js:127:8)
    at request (.../node_modules/request/index.js:53:10)
    at Request._callback (.../downloadImg.js:7:5)
    at self.callback (.../node_modules/request/request.js:185:22)
    at Request.emit (events.js:310:20)
    at Request.init (.../node_modules/request/request.js:273:17)
    at new Request (.../node_modules/request/request.js:127:8)
    at request (.../node_modules/request/index.js:53:10)
    at Function.head (.../node_modules/request/index.js:61:12)
Emitted 'error' event on Request instance at:
    at Request.init (.../node_modules/request/request.js:273:17)
    at new Request (.../node_modules/request/request.js:127:8)
    [... lines matching original stack trace ...]
    at Function.head (.../node_modules/request/index.js:61:12)
user12669401
  • 229
  • 4
  • 12

2 Answers2

1

Can you change this code from

var download = function(uri, filename, callback){
  request.head(uri, function(err, res, body){
    request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
  });
};

to

var download = function(uri, filename, callback){
  if(uri !== undefined){
    request.head(uri, function(err, res, body){
      request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
    });
  }
};
FZs
  • 16,581
  • 13
  • 41
  • 50
RAJENDRA H
  • 83
  • 10
  • You will have to check for `"undefined"` (a string) instead. OP's code stringifies `undefined` by `\`${imgUrl}\`` – FZs May 01 '20 at 06:31
  • @FZs However it doesn't matter until it reaches the download function, when `${imgUrl}` goes into download function as a parameter(URI) if it is a undefined then it through error. so its better to check using if condition. to get rid of error – RAJENDRA H May 01 '20 at 06:38
  • When it gets to the `download` function, it **is a string**. The error caused by its *syntax*, not its *type*, as `"undefined"` is (obviously) not a valid URL. Your check won't filter out the case in which the OP's code fails. – FZs May 01 '20 at 06:41
  • 1
    the URL was valid; so I just changed my code and it worked. Posting the working code here so it might be useful for someone else looking for the same thing – user12669401 May 02 '20 at 22:39
0

Changed the code to this and it worked:

for (var i = 0; i < 11952; i++) { 

    let imgName = allProducts[i].code;    
    let imgUrl = allProducts[i].nofrillsImgLink    

      request({
        url : `${imgUrl}`,
        //make the returned body a Buffer
        encoding : null
    }, function(error, response, body) {

        //will be true, body is Buffer( http://nodejs.org/api/buffer.html )
        console.log(body instanceof Buffer);

        //do what you want with body
        //like writing the buffer to a file
        fs.writeFile(`./allProductsImg/${imgName}.png`, body, {
            encoding : null
        }, function(err) {

            if (err)
                throw err;
            console.log('It\'s saved!');
        });

    });

};
user12669401
  • 229
  • 4
  • 12