Best way to fetch referenced entities inside an entry with JavaScript SDK

  • 28 July 2022
  • 1 reply
  • 206 views

Userlevel 1
Badge

Referenced entities only return the UID of their Content Type by default. the SDK’s Stack.Entry has an includeReference() method, but you must provide it with the UIDs of referenced entities. For dynamic cases, what is the most elegant way to return referenced entity data using the JavaScript SDK?


1 reply

Userlevel 4

Hey Will,

Generally, references are set inside the content schema, so beforehand you already know the reference fields that are possible for each content type. So for example, a content type has author, category, & related_post fields for the user to work with. You already know the possible references field UID’s, so you can include them as such:

.includeReference(['author', 'category', 'related_post'])

The JSON Response will return all the data about those references, but if there is no reference set on the UI side, then they will return [].

What use cases are you looking to solve? 

Reply