Skip to main content

Call the nodes.get() method in our delivery client to return a node from site view, optionally resolving an attached entry

Call signatures

TypeScript
get(id: string): Promise<Node>

get(path: string): Promise<Node>

get(options: NodeGetByIdOptions): Promise<Node>

get(options: NodeGetByPathOptions): Promise<Node>

Parameters

NameTypeDescription
idstringThe id of the node
pathstringThe full path to the node
optionsNodeGetByIdOptionsAn options object to apply additional refinements
optionsNodeGetByPathOptionsAn options object to apply additional refinements

Returns

A Promise that will resolve with the Node or array of Node

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

TypeScript
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

TypeScript
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

TypeScript
getByEntry(entryId: string): Promise<Node[]>

getByEntry(entry: Entry): Promise<Node[]>

getByEntry(options: NodeGetByEntryOptions): Promise<Node[]>

Parameters

NameTypeDescription
entryIdstringThe id of the entry attached to the node(s)
entryEntryAn entry object containing the sys.id of the entry attached to the node(s)
optionsNodeGetByEntryOptionsAn 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

TypeScript
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

TypeScript
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);
}

Still need help?

If you still need help after reading this article, don't hesitate to reach out to the Contensis community on Slack or raise a support ticket to get help from our team.
New support request