0
var request = require("request").defaults({ encoding: null });

var picturesArray = [];
request.get('http://www.vorohome.com//images/assets/159314_887955.png', function (error, response, body) {
    if (!error && response.statusCode == 200) {
        data = "data:" + response.headers["content-type"] + ";base64," + new Buffer(body).toString('base64');
        console.log(data)
        picturesArray[0] = data;
    }
});
console.log(picturesArray);
hsz
  • 148,279
  • 62
  • 259
  • 315
Kunal
  • 603
  • 4
  • 13
  • @Armin — That's terrible advice. There's a callback function will fill fire when the data is available. Guessing how long it will take to become available is just stupid. – Quentin Jul 30 '18 at 11:33
  • But guys any suggestion how I can collect the data in pictureArray? – Kunal Jul 30 '18 at 11:35
  • See the duplicate question linked in the big yellow box at the top of the page. – Quentin Jul 30 '18 at 11:39

1 Answers1

1

Because you're calling

console.log(picturesArray);

before asynchronous action is finished.

If you'll add this log inside the request callback - you'll get the proper result.

hsz
  • 148,279
  • 62
  • 259
  • 315