Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 431
user1452221 Avatar asked Jan 30 '23 09:01

user1452221


2 Answers

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).

like image 149
Jakub Synowiec Avatar answered Jan 31 '23 22:01

Jakub Synowiec


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

like image 39
Anton Romankov Avatar answered Jan 31 '23 22:01

Anton Romankov