Skip to content
Advertisement

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?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement