kumorin
kumorin

Reputation: 83

Strapi multi-level population with field selection

I want to populate a multilevel relational field on strapi with REST API with specific field selection, but only the id comes up at the response.

When I populate the field without field selections with a GET request at: localhost:1337/api/home?populate[0]=Reasons&populate[1]=Reasons.IconsList&populate[2]=Reasons.IconsList.Icon, I get this JSON response:

{
    "data": {
        "id": 1,
        "attributes": {
            "createdAt": "2022-10-31T11:24:55.202Z",
            "updatedAt": "2022-11-09T13:50:40.445Z",
            "publishedAt": "2022-10-31T11:26:30.944Z",
            "PageInfo": "Homepage",
            "Reasons": {
                "id": 1,
                "IconsList": [
                    {
                        "id": 1,
                        "Content": "Free cancellation on most bookings",
                        "Icon": {
                            "data": {
                                "id": 3,
                                "attributes": {
                                    "name": "cancellation.svg",
                                    "alternativeText": "Free cancellation on most bookings",
                                    "caption": "cancellation.svg",
                                    "width": 21,
                                    "height": 21,
                                    "formats": null,
                                    "hash": "cancellation_b2308d4b63",
                                    "ext": ".svg",
                                    "mime": "image/svg+xml",
                                    "size": 1.57,
                                    "url": "/uploads/cancellation_b2308d4b63.svg",
                                    "previewUrl": null,
                                    "provider": "local",
                                    "provider_metadata": null,
                                    "createdAt": "2022-10-20T12:02:37.735Z",
                                    "updatedAt": "2022-10-31T11:23:48.326Z"
                                }
                            }
                        }
                    },
                    {
                        "id": 2,
                        "Content": "No credit card fees",
                        "Icon": {
                            "data": {
                                "id": 5,
                                "attributes": {
                                    "name": "credit-card.svg",
                                    "alternativeText": "credit-card.svg",
                                    "caption": "credit-card.svg",
                                    "width": 22,
                                    "height": 16,
                                    "formats": null,
                                    "hash": "credit_card_06b9eca40f",
                                    "ext": ".svg",
                                    "mime": "image/svg+xml",
                                    "size": 0.89,
                                    "url": "/uploads/credit_card_06b9eca40f.svg",
                                    "previewUrl": null,
                                    "provider": "local",
                                    "provider_metadata": null,
                                    "createdAt": "2022-10-20T12:21:46.342Z",
                                    "updatedAt": "2022-10-20T12:21:46.342Z"
                                }
                            }
                        }
                    },
                    {
                        "id": 3,
                        "Content": "24/7 Customer care",
                        "Icon": {
                            "data": {
                                "id": 2,
                                "attributes": {
                                    "name": "customer-care.svg",
                                    "alternativeText": "24/7 Customer care",
                                    "caption": "customer-care.svg",
                                    "width": 20,
                                    "height": 22,
                                    "formats": null,
                                    "hash": "customer_care_6aa8ac8eea",
                                    "ext": ".svg",
                                    "mime": "image/svg+xml",
                                    "size": 1.48,
                                    "url": "/uploads/customer_care_6aa8ac8eea.svg",
                                    "previewUrl": null,
                                    "provider": "local",
                                    "provider_metadata": null,
                                    "createdAt": "2022-10-20T12:02:37.734Z",
                                    "updatedAt": "2022-10-20T12:04:39.792Z"
                                }
                            }
                        }
                    }
                ]
            }
        }
    },
    "meta": {}
}

Following this guide: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest/populating-fields.html#combining-population-with-other-operators, when I try to define the fields I want from the response: localhost:1337/api/home?fields[0]=PageInfo&populate[Reasons][IconsList]fields]=Content, I only get this JSON:

{
    "data": {
        "id": 1,
        "attributes": {
            "PageInfo": "Homepage",
            "Reasons": {
                "id": 1
            }
        }
    },
    "meta": {}
}

I want to get IconsList's Content and Icon.alternativeText and Icon.url but I cannot find how to build my query parameters.

Upvotes: 1

Views: 2543

Answers (1)

kumorin
kumorin

Reputation: 83

For future reference to this problem, my question was answered on Strapi forum: https://forum.strapi.io/t/strapi-multi-level-population-with-field-selection/23473

This tutorial explains the reasoning behind deep populating and filtering and it's really interesting.

The problem was that you need to pupulate the relational field again like that: localhost:1337/api/home?fields[0]=PageInfo&populate[Reasons][populate][IconsList][fields][0]=Content to be able to access its data.

Upvotes: 1

Related Questions