Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug]: Populate multiple levels not working as expected #26

Closed
3 tasks done
douwepausma opened this issue Jan 10, 2025 · 5 comments
Closed
3 tasks done

[bug]: Populate multiple levels not working as expected #26

douwepausma opened this issue Jan 10, 2025 · 5 comments
Assignees
Labels
issue: bug Issue reporting a bug status: to be confirmed The issue needs to be reproduced or confirmed by a team member

Comments

@douwepausma
Copy link
Contributor

douwepausma commented Jan 10, 2025

Bug Description

At first I thought I need to use an array to populate much like the other Strapi API's, but when I hover the populate attribute (see the picture at expected behavior) I noticed I need to use an object. However it only works when ik go no further than 1 level.

This works

const results = await strapi.collection('pages').find({
        populate: {
            seo: true
        }
});

But this doesn't

const results = await strapi.collection('pages').find({
        populate: {
            seo: {
                metaImage: true
            }
        }
});

Steps to Reproduce

Try and fetch entries with a populate level deeper than 1.

const results = await strapi.collection('pages').find({
        populate: {
            seo: {
                metaImage: true
            }
        }
});

Expected Behavior

To populate multiple levels.

Version

1.0.0-beta.0

Operating System

MacOS

Runtime Environment

Node.js

Logs

⨯ HTTPBadRequestError: Request failed with status code 400 Bad Request: GET http://127.0.0.1:1337/api/pages?filters%5Bpathname%5D=%2Ftrainingen%2Fabout-me&populate%5Bseo%5D%5BmetaImage%5D=true
    at async getPageByPathname (./src/actions/strapi/page.ts:15:21)
    at async Page (./src/app/[...slug]/page.tsx:34:5)
digest: "2498158417"

TRYING THE REQUEST IN POSTMAN:
{
    "data": null,
    "error": {
        "status": 400,
        "name": "ValidationError",
        "message": "Invalid key metaImage at seo.metaImage",
        "details": {
            "key": "metaImage",
            "path": "seo.metaImage",
            "source": "query",
            "param": "populate"
        }
    }
}

Media

populate description

image

Confirmation Checklist

  • I have checked the existing issues
  • I agree to follow this project's Code of Conduct
  • I would like to work on this issue
@douwepausma douwepausma added issue: bug Issue reporting a bug status: to be confirmed The issue needs to be reproduced or confirmed by a team member labels Jan 10, 2025
@douwepausma
Copy link
Contributor Author

I've created a pull request to resolve this issue. Apparently string arrays are support, but Typescript was saying otherwise. So I added the proper type.

#28

@Convly
Copy link
Member

Convly commented Jan 13, 2025

Thanks for opening this issue, I'll have a look to your issue and PR to check what's happening

@Convly
Copy link
Member

Convly commented Jan 28, 2025

Hey, I tried reproducing your issue, but ended up spotting what I think is a typo in your query.

Could you try again with

const results = await strapi.collection('pages').find({
        populate: {
            seo: {
                populate: {
                    metaImage: true
                }
            }
        }
});

@douwepausma
Copy link
Contributor Author

You are right! That does work, thanks! I'll be sticking to the string[] for now though, as I think it is a bit cleaner. I would favour the object notation if I didn't have to re-emphasize populate every level.

@Convly
Copy link
Member

Convly commented Jan 30, 2025

Noted, thanks for your feedback and good luck in your project! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Issue reporting a bug status: to be confirmed The issue needs to be reproduced or confirmed by a team member
Projects
None yet
Development

No branches or pull requests

2 participants