question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best practice accessing populated fields in Strapi4 response

See original GitHub issue

Hello 😃

I was just playing around and saw that populated fields are returned with the same structure as the “parent”

   export interface Strapi4ResponseData<T> {
     id: number,
     attributes: T,
     meta: Record<string, unknown>
   }

Accessing such a field would result in response.data[0].attributes.populated_field.data.attributes

In my case, I populated a media field containing one picture.

Quite the difference from the Strapi3 response.

What would you recommend in terms of storing such a response in store (I’m currently using pinia) and accessing the data as efficient as possible?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
benjamincanaccommented, Jan 20, 2022

Closing this as I dont have any more informations on how to achieve this.

@luke-z If you feel your solution is ready, don’t hesitate to make a PR to the documentation in Advanced section to explain how to achieve this, maybe the solution described here https://github.com/nuxt-community/strapi-module/issues/204 can be usefull too.

0reactions
jch-codecommented, Apr 21, 2022

I created a tool to handle this issue for myself, strapi-parse.

I have only used it on a small sample size in my own projects, so I wouldn’t be surprised if I missed some edge cases, but it works for me on some fairly complex objects with populated fields, dynamic zones, & repeatable components.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Discussion regarding default population of REST
Current status on v4: We will not auto populate any field by default within Strapi as the purpose for not doing so is...
Read more >
Strapi V4 populate Media and Dynamiczones from Components
in my article schema i have a dynamiczone with 3 components blog.image, blog.rich-text and blog.gallery. When i call the rest api by “/articles?...
Read more >
Population for REST API - Strapi Developer Docs
Use the populate parameter to populate specific fields and the select parameter to return only specific fields with the query results.
Read more >
Strapi V4 populate Media and Dynamiczones from Components
Hello, in my article schema i have a dynamiczone with 3 components blog.image, blog.rich-text and blog.gallery. When i call the rest api by ......
Read more >
How to Filter Results on Deeply Nested fields in Strapi
Learn about different ways to deeply filter results using Strapi's REST, Entity Service, Query Engine and GraphQL API.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found