'Faker shows the same picture all the time, how to avoid it?

I am using faker in order to make array of random objects, like this:

{
  "image":  faker.random.arrayElement([
    faker.image.nature(), 
    faker.image.city(), 
    faker.image.food() 
  ]),
  "price": faker.random.number({ min: 20, max: 300 }),
  "beds": faker.random.number({ min: 1, max: 15 }),
  "type": faker.random.arrayElement([ 
    "Entire home", 
    "Private room", 
    "Shared room" 
  ])
}

Actully what happens when making 12 elements like this - all the data is random but not the image, that is the same in every load.

Every refresh there are other photoes, but all the elements in array constains the same image.

What shall I do?



Solution 1:[1]

To be precise, faker.js is returning the same url, e.g. http://lorempixel.com/640/480/nature for faker.image.nature() call because it's the lorempixel.com service that returns random images on request.

If you're rather referring to seeing exactly the same image then this might be related to your browser caching responses (if so, then try disabling cache during development or adding a random query string to the image, e.g. 'image': `${faker.image.nature()}?random=${Date.now()}`).

So to answer your question - you don't have to do anything. You'll get random images eventually (on request to lorempixel.com).

Solution 2:[2]

Looked for the same. Found this in the docs.

faker.image.nature(width, height, randomize)

Parameters | Name | Type | Default | |------------|-------|---------| | width? |number | 640 | | height? |number | 480 | | randomize? |boolean| false |

Source: https://fakerjs.dev/api/image.html#nature

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Griffin