1

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

2
  • Sorry, the question is a bit unclear. So you have 2 collections i.e blog-articles & blog-categories which are related with many-to-many relation. What do you need in your response of fetch API? What do you mean by 'I'm trying to make a "related articles" for a "blog-article" single based off the relation "blog-categories"?' Commented Jan 12, 2022 at 16:16
  • @Salvino Sorry for the unclear question. My question is: how do I get the related blog-categories field of blog-articles with the api fetch call. Commented Jan 12, 2022 at 17:25

1 Answer 1

0

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Doesn't work unfortunately. Shows the same information as before, except without the thumbnail information this time.
Could you post your blog-articles model setting json file in your question?
Added the schema.json file to the original post. Here's the specific field: "attributes": { "blog_categories": { "type": "relation", "relation": "manyToMany", "target": "api::blog-category.blog-category", "inversedBy": "blog_articles" },
Yep, my query seems to be correct. Could you check for the data in categories? Check if there are published records..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.