1

I am a learner of JavaScript here. I have a file called downloadFakeImage.js with the following script:

const faker = require('faker');
const axios = require('axios');
const path = require('path');
const fs = require('fs');

const url = [];
// Generate 1000 fake url from faker
for (let i=0; i<1000; i++) {
    url.push(faker.image.fashion());
}

// Download 1000 user photos to local image folder
for (let i=0; i<url.length; i++) {
    // path for image storage
    const imagePath = path.join(__dirname, './image', `${i}.jpg`);
    axios({
        method: 'get',
        url: url[i],
        responseType: 'stream'
    })
        .then((response) => {
            response.data.pipe(fs.createWriteStream(imagePath));
        });
}

The goal of this script is to generate 1000 fake fashion images to a folder named image. When I run node downloadFakeImage.js in my terminal, only some of the images get to save into the folder. My terminal show whole brunch of the following error message:

Error message from the terminal that I received

I think this might be related to an async issue, can someone teaches me how to refractor my script to make it work?

Update:

I refactored my code to the following and I was able to generate some images, but I still cannot generate 1000 images. For the first 300 images, it was running ok and then it failed.

const faker = require('faker');
const axios = require('axios');
const path = require('path');
const fs = require('fs');

async function seedImage() {
    const url = [];
    // Generate 1000 fake url from faker
    for (let i = 0; i < 1000; i++) {
        url.push(faker.image.fashion());
    }

    // Download 1000 user photos to local image folder
    for (let i = 0; i < url.length; i++) {
        // path for image storage
        const imagePath = await path.join(__dirname, './image', `${i}.jpg`);
        axios({
            method: 'get',
            url: url[i],
            responseType: 'stream'
        })
            .then((response) => {
                response.data.pipe(fs.createWriteStream(imagePath));
            })
            .catch((error) => {
                console.log(error);
            });
    }
}

seedImage();
hongkongbboy
  • 300
  • 1
  • 5
  • 14
  • 1
    Add a `catch` callback to the Promise chain, do whatever with the rejected Promise error, or drop and ignore it. Does this answer your question? [What is an unhandled promise rejection?](https://stackoverflow.com/questions/40500490/what-is-an-unhandled-promise-rejection) – Drew Reese May 22 '20 at 08:04
  • Does this answer your question? [What is an unhandled promise rejection?](https://stackoverflow.com/questions/40500490/what-is-an-unhandled-promise-rejection) – Wiktor Zychla May 22 '20 at 08:17
  • you do realise you're trying to make 1000 simultaneous requests, right? perhaps the endpoint is saying "no way", and giving you errors, which you don't catch – Jaromanda X May 22 '20 at 08:36
  • I was able to catch the error using the help from below. However, I wasn't able to figure out how to solve this issue from reading the error. It is probably my script is not written correctly. I might have to learn how to use Unsplash API instead. – hongkongbboy May 22 '20 at 13:37

1 Answers1

0

You can add a catch block to handle errors as below:

const faker = require('faker');
const axios = require('axios');
const path = require('path');
const fs = require('fs');

const url = [];
// Generate 1000 fake url from faker
for (let i=0; i<1000; i++) {
    url.push(faker.image.fashion());
}

// Download 1000 user photos to local image folder
for (let i=0; i<url.length; i++) {
    // path for image storage
    const imagePath = path.join(__dirname, './image', `${i}.jpg`);
    axios({
        method: 'get',
        url: url[i],
        responseType: 'stream'
    })
        .then((response) => {
            response.data.pipe(fs.createWriteStream(imagePath));
        })
        .catch((error) => {
          console.log(error);
        });
}
Uyen Do
  • 9
  • 2
  • Thank you. I was able to catch the error using this method. However, the error message is not helpful for me to understand how to debug it. Thank you though. – hongkongbboy May 22 '20 at 13:35
  • I think the problems are you request too much to server. You can reduce the number URL to 3 for the test or set the delay time for each request by use *timeout* function. – Uyen Do May 22 '20 at 14:45
  • I updated the post with my refactored code. The script running okay for the first 300 images and then it failed. – hongkongbboy May 23 '20 at 00:27
  • I think the script should work for a small amount of images. However, my goal is to get a large amount of images. – hongkongbboy May 23 '20 at 00:28