Fetch Linked Document Fields with Javascript

Here we explore an example that fetches a specific content field from a linked document.

The fetchLinks option allows you to retrieve a specific content field from a linked document and add it to the document response object.

Note that this only works with basic fields such as Text, Number, or Date. It is not possible to retrieve Rich Text fragments from the linked document using this field.

The value you enter for the fetchLinks option needs to take the following format:

Copy
{ 'fetchLinks' : '{custom-type}.{field}' }

{custom-type}

The custom type API-ID of the linked document

{field}

The API-ID of the field you wish to retrieve from the linked document

A simple example

The following is an example that uses the fetchLinks option.

It shows how to query for the recipe with the uid "chocolate-chip-cookies". If the custom type "recipe" has a link to another custom type "author," then you can pull in certain fields from that linked document, in this case the "name" field.

Copy
//To retreive the API object check how to query the API
api.getByUID('recipe', 'chocolate-chip-cookies',
    {'fetchLinks': 'author.name'}
).then(function(document) {
    var author = document.getLink('recipe.author-link');
    // variable author now works like a top-level document
    var authorName = author.getText('author.name');
    console.log(authorName);
});