The Response Object in Javascript
After you set up your Custom Types and have queried your content from the API, it’s time to integrate that content into your templates. First we’ll go over the response object returned from the API, then we’ll discuss how to retrieve your documents.
The Response Object
Let’s start by taking a look at the Response Object returned when querying the API. Here is a simple example of response object with one document that contains a couple of fields.
{
"page": 1,
"results_per_page": 20,
"results_size": 1,
"total_results_size": 1,
"total_pages": 1,
"next_page": null,
"prev_page": null,
"results": [
{
"id": "WKxlPCUEEIZ10AHU",
"uid": "example-page",
"type": "page",
"href": "https://your-repo-name.prismic.io/api/documents/search...",
"tags": [],
"first_publication_date": "2017-01-13T11:45:21.000Z",
"last_publication_date": "2017-02-21T16:05:19.000Z",
"slugs": [
"example-page"
],
"linked_documents": [],
"lang": "en-us",
"alternate_languages": [
{
"id": "WZcAEyoAACcA0LHi",
"uid": "example-page-french",
"type": "page",
"lang": "fr-fr"
}
],
"data": {
"title": [
{
"type": "heading1",
"text": "Example Page",
"spans": []
}
],
"date": "2017-01-13"
}
}
]
}
At the topmost level of the response object, you mostly have information about the number of results returned from the query and the pagination of the results.
Note that when using certain helper functions such as getSingle(), getByUID(), or getByID(), the results will automatically be returned.
The Query Results
The actual content of the returned documents can be found under "results". This will always be an array of the documents, even if there is only one document returned.
Let’s say that you saved your response object in a variable named "response". This would mean that your documents could be accessed with the following:
var documents = response.results;
And if you only returned one document, it would be accessed with the following:
var document = response.results[0];
Each document will contain information such as it’s document ID, UID, type, tags, slugs, first publication date, & last publication date.
The content for each document will be found inside data. In the example above you have title and date.