Blue Moon
Blue Moon

Reputation: 99

Strapi: How to include/populate relations with fetch call?

I'm trying to make a "related articles" for a "blog-article" single based off the relation "blog-categories", but I can't seem to get the relations to appear in the api call. What's the API url with the correct parameters to get relations with Strapi?

To populate all of the articles in descending order by date with images, I'm doing:

const res = 
   await fetch (
       https://localhost:3000/api/blog-articles?populate=*&sort[0]=publishDate%3Adesc
   )

Collection's Content Fields

Edit: Fetch result Edit 2: Schema

Upvotes: 1

Views: 1664

Answers (1)

Salvino D'sa
Salvino D'sa

Reputation: 4506

I think you can achieve this by passing the related field name in the populate http query string.

HTTP Request URL
GET http://localhost:3000/api/blog-articles?populate=blog_categories&sort[0]=publishedAt%3Adesc
JS Implementation
const resp = await fetch('http://localhost:3000/api/blog-articles?populate=blog_categories&sort[0]=publishedAt%3Adesc');
const data = await resp.json();
console.log(data);

Upvotes: 0

Related Questions