Ammar Uppal
Ammar Uppal

Reputation: 579

Media field is not showing up in api response of strapi v4

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.

Upvotes: 12

Views: 9848

Answers (3)

Avi Siboni
Avi Siboni

Reputation: 804

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.

Upvotes: 3

Kushal Gurung
Kushal Gurung

Reputation: 1

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

Upvotes: -2

Ammar Uppal
Ammar Uppal

Reputation: 579

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=*

Upvotes: 34

Related Questions