Posts with Related Posts of the same content type produce very deep object
See original GitHub issueExpected Behavior
When fetching post data with include: 3
, response should not return data deeper than 3 levels.
Actual Behavior
export async function getPosts(slug, preview) {
const entries = await contentfulClient(preview).getEntries({
content_type: 'post',
'fields.slug[in]': slug,
include: 3
});
return entries;
}
The following returns:
items: {
0: {
fields: {
title: 'article 1',
relatedPosts: {
0: {
fields: {
title: 'article 2',
relatedPosts: {
0: {
fields: {
title: 'article 3',
relatedPosts: {
0: {
fields: {
title: 'article 4',
relatedPosts: [...goes on very deep...]
}
}
}
}
}
}
}
}
}
}
}
}
- Node Version: 14.13.1
- Yarn Version: 1.22.10
- Operating System: OSX Chrome
- Package Version: 7.14.8
- Which API are you using?: Delivery
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
WordPress Custom Post Types: The All-In-One Guide ... - Kinsta
Learn exactly what WordPress custom post types are, how they're different from standard posts, and how to create and use them to extend...
Read more >YARPP – Yet Another Related Posts Plugin - WordPress.org
The best WordPress plugin for displaying related posts. Simple and flexible, with a powerful proven algorithm and inbuilt caching.
Read more >Ultimate Guide to Custom Post Types in WordPress
The ultimate guide to WordPress post types with custom fields, taxonomies, icons, and more.
Read more >3 Ways to Clone Objects in JavaScript | SamanthaMing.com
# 1. Using Spread. Using spread will clone your object. Note this will be a shallow copy. As of this post, the spread...
Read more >POST JSON fails with 415 Unsupported media type, Spring 3 ...
I've had this happen before with Spring @ResponseBody and it was because there was no accept header sent with the request. Accept header...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @AlexandraKlein could it be that the
relatedPosts
contain circular references?While
include: 3
only fetches three levels deep, if entries reference each other, they are linked together so a circular reference might look like infinite depths when logged.From the thread it sounds like we can close the issue, correct?
Sadly there isn’t really a way to stop circular dependencies after a certain level.
The entry objects that link to each other are always the same instances, so there is no way to break the cycle at some point.
Take for example a list of objects linking like this
A -> B -> C -> A -> B ...
.The instance of
A
that links toB
is the same as the oneC
links to.If I were to remove the reference from
A
toB
to break the cycle at the point where it repeats, I would break the original chain too.If you want to work with a fully flattened version of the model, you would have to use the raw response and manually resolve the
includes
from the returned entries where you can keep track of cycles yourself.