最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Faker shows the same picture all the time, how to avoid it? - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Oct 18, 2017 at 7:33 Orlandster 4,8584 gold badges34 silver badges45 bronze badges asked Oct 18, 2017 at 7:12 user1452221user1452221 1242 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

To be precise, faker.js is returning the same url, e.g. http://lorempixel./640/480/nature for faker.image.nature() call because it's the lorempixel. 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.).

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

发布评论

评论列表(0)

  1. 暂无评论