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.

Posts with Related Posts of the same content type produce very deep object

See original GitHub issue

Expected 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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
TimBeyercommented, Nov 9, 2020

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.

1reaction
TimBeyercommented, Nov 10, 2020

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 to B is the same as the one C links to.
If I were to remove the reference from A to B 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.

Read more comments on GitHub >

github_iconTop 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 >

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