I have a problem where none of the relational fields are present in the responses after fetching my data. When I look to the schema of one of my schemas with a relation, I see that the relational fields are present in the attributes object. But still I only get the non-relational fields in my response.
This is one of my schemas
{
"kind": "collectionType",
"collectionName": "activities",
"info": {
"singularName": "activity",
"pluralName": "activities",
"displayName": "activity"
},
"options": {
"draftAndPublish": true
},
"pluginOptions": {},
"attributes": {
"name": {
"type": "string"
},
"date": {
"type": "date"
},
"subcategory": {
"type": "relation",
"relation": "oneToOne",
"target": "api::subcategory.subcategory"
},
"members": {
"type": "relation",
"relation": "manyToMany",
"target": "api::member.member",
"inversedBy": "activities"
}
}
}
In Strapi v4 relations are not populated when fetching entries by default.
Queries can accept a populate parameter to explicitly define which fields to populate, with the following syntax:
GET /api/:pluralApiId?populate=field1,field2
GET /api/books?populate=author.name,author.address
For convenience, the * wildcard can be used to populate all first-level relations:
GET /api/books?populate=*
GET /api/books?populate[author]=*
Note: Only first-level relations are populated with populate=*. Use the LHS bracket syntax (i.e. [populate]=*) to populate deeper:
GET /api/global?populate[navigation][populate]=*
Change your API url to one of the following and you should be able to see the related fields populated in the response.
GET /api/activities?populate=subcategory,members
OR
GET /api/activities?populate=*