Query Documents by ID or UID in Javascript

This page gives examples of how to query for a document either by its ID or its UID. It provides examples for both using the query helper functions and doing the query manually.

Query by ID

With the helper function

Here is an example showing how to query a document by its id using the helper function.

Copy
api.getByID('WAjgAygABN3B0a-a').then(function(pageContent) {
    // pageContent contains the document content
});

Without the helper function

Here is an example of how to query a document by its id without the helper function.

Copy
//To retreive the API object check how to query the API
api.query(
    Prismic.Predicates.at('document.id', 'WAjgAygAAN3B0a-a')
).then(function(pageContent) {
    // pageContent.results[0] contains the document content
});

Query multiple documents by their IDs

With the helper function

Here is an example of how to query multiple documents by their ids using the helper function.

Copy
var ids = ['WAjgAygAAN3B0a-a', 'WC7GECUAAHBHQd-Y', 'WEE_gikAAC2feA-z'];
//To retreive the API object check how to query the API
api.getByIDs(ids).then(function(content) {
    // content is the response object, content.results holds the documents
});

Without the helper function

Query multiple documents by their ids without the helper function

Copy
var ids = ['WAjgAygAAN3B0a-a', 'WC7GECUAAHBHQd-Y', 'WEE_gikAAC2feA-z'];
//To retreive the API object check how to query the API
api.query(
    Prismic.Predicates.in('document.id', ids)
).then(function(content) {
    // content is the response object, content.results holds the documents
});

Query by UID

With the helper function

Here is an example of how to query a document of the type “page” by its uid “about-us”.

Copy
//To retreive the API object check how to query the API
api.getByUID('page', 'about-us').then(function(pageContent) {
    // pageContent contains the document content
});

Without the helper function

Here is an example of how to query a document of the type "page" by its uid "about us" without using the helper function.

Copy
//To retreive the API object check how to query the API
api.query(
    Prismic.Predicates.at('my.page.uid', 'about-us')
).then(function(pageContent) {
    // pageContent is the response object, pageContent.results holds the documents
});