7

I have a media field in my collection of strapi (v4) server named Picture but it doesn't come up in api response when I request localhost:1337/api/products.

Ammar Uppal
  • 469
  • 4
  • 6
  • 1
    Does this answer your question? [Components not included in Strapi api response](https://stackoverflow.com/questions/70249364/components-not-included-in-strapi-api-response) – IceJonas Dec 21 '21 at 17:38

3 Answers3

28

Solved, yay!

Documentation of Strapi v4 says:

Relations population

By default, relations are not populated when fetching entries.

Queries can accept a populate parameter to explicitly define which fields to populate, with the following syntax:

GET /api/books?populate=*

So I had to just postfix my GET request with ?populate=*

Answer: localhost:1337/api/products?populate=*

Ammar Uppal
  • 469
  • 4
  • 6
2

After reading Strapi Docs - here, turn out you can populate directly the media fields -

const qs = require('qs');
const query = qs.stringify({
  populate: [
    'seoData',
    'seoData.sharedImage',
    'seoData.sharedImage.media',
  ],
}, {
  encodeValuesOnly: true,
});

await request(`/api/articles?${query}`);

instead of populating all(by *) which is not a good practice because,

  1. Data classification - In most cases, you don't need to return the entire payload such as when the collection is published and so forth.
  2. Performance - When you select only the necessary ones, your query to the DB will be more minor and therefore the result will come faster.
  3. Reduce payload - When you select only the necessary ones, you reduce the payload that returns to the client.
Avi Siboni
  • 686
  • 7
  • 16
-2

at last write, populate=picture(name of media field)

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 31 '22 at 12:07
  • Doesn't work for me – Avi Siboni Jun 12 '22 at 20:18