How to fetch referenced entries using Javascript SDK.


Userlevel 1

Topic or Problem Statement

Fetch referenced entries using Javascript SDK.

 

Root Cause or Use Case

While fetching the entries through SDK only the uid of the referenced entry is fetched. User will have to include the references in the response if they wish to get the referenced entry.

 

Prerequisites to Resolve

  • The stack API key, delivery token, Environment name, Knowledge of Javascript SDK.

 

Steps to Resolve

When fetching an entry, the content of referred entries that are part of the parent entry is NOT included in the response body, you only get their UIDs. To include the content of the referred entries in your response, you need to use the includeReference() parameter and specify the UID of the reference field as value. Below is an example of the JS-SDK query with includeReference() parameter.

 

// Initialize the Contentstack Stack

const Contentstack = require('contentstack')



const Stack = Contentstack.Stack("stack_api_key", "delivery_token", "environment_name");

var Query = Stack.ContentType(‘contentType_uid’).Query();



Query

    .includeReference('referenceField_uid')

    .toJSON()

    .find()

    .then(function success(result) {

        console.log(result);       

    })

 

Related Questions or Symptoms

  1. Is it possible to include the reference field json content instead of just the uid while fetching the entry using JS-SDK?

 

Other Helpful Resources


3 replies

Userlevel 1
Badge

+1 on this one. It would be awesome if the includeReference() method with no arg defaulted to including all content type UIDs as references

Userlevel 1

Thanks for the suggestion, Will! As of today, include_all is not available in SDKs but we are looking to add this parameter in future.

Appreciate your valuable feedback here 😊

Hi there! Any update on the include_all feature? My team is looking to use deeply nested references and this would make things much easier for us.

Reply