Getting nodes
Log in to add to favouritesCall the nodes.get() method in our delivery client to return a node from site view, optionally resolving an attached entry
Call signatures
get(id: string): Promise<Node>
get(path: string): Promise<Node>
get(options: NodeGetByIdOptions): Promise<Node>
get(options: NodeGetByPathOptions): Promise<Node>
Parameters
Name | Type | Description |
---|---|---|
id | string | The id of the node |
path | string | The full path to the node |
options | NodeGetByIdOptions | An options object to apply additional refinements |
options | NodeGetByPathOptions | An options object to apply additional refinements |
Returns
Remarks
Throws any error that is returned by the API for any unsuccessful request echoing the HTTP status returned from the API
Example
Get a node by its ID
const node = await client.nodes.get("57d77dbc-49da-40ca-aaaf-3b877b69699a");
Get a node by a path, resolving all fields in any entry attached to the found node while handling any errors
try {
const node = await client.nodes.get({
path: "/about/contact",
entryFields: ["*"],
});
if (node) {
// the node is available
console.log(node.path);
if (node.entry) {
// the node has an entry attached
console.log(node.entry.entryTitle);
}
}
} catch (error) {
if ("status" in error)
// format the error message returned from the api
console.error(
`[${error.status} ${error.statusText}] ${error.data?.message}`
);
// log the raw error
else console.error(error);
}
Get nodes by entry
Call the nodes.getByEntry() method in our delivery client to return a node array from site view, optionally resolving any attached entry
Call signatures
getByEntry(entryId: string): Promise<Node[]>
getByEntry(entry: Entry): Promise<Node[]>
getByEntry(options: NodeGetByEntryOptions): Promise<Node[]>
Parameters
Name | Type | Description |
---|---|---|
entryId | string | The id of the entry attached to the node(s) |
entry | Entry | An entry object containing the sys.id of the entry attached to the node(s) |
options | NodeGetByEntryOptions | An options object to apply additional refinements |
Returns
A Promise that will resolve with an array of Node
Remarks
Returns an empty array if no nodes exist with the specified entry
Example
Get all nodes that have the entry with the supplied entry ID attached
const nodes = await client.nodes.getByEntry(
"ba950397-5e7c-4847-a07f-cf015b9e59cb"
);
if (nodes.length > 0) {
console.log(nodes[0].path);
} else {
console.error("no nodes were found");
}
Get all nodes that have the supplied entry attached
import type { Entry, EntrySys } from "contensis-delivery-api";
const entry: Entry = {
entryTitle: "An entry",
sys: {
id: "ba950397-5e7c-4847-a07f-cf015b9e59cb",
contentTypeId: "movie",
} as EntrySys,
};
const nodes = await client.nodes.getByEntry(entry);
for (const node of nodes) {
console.log(node.path);
}