{"record":{"id":"409361970074f712","repo":"ianstormtaylor/slate","slug":"cannot-get-child-at-index-index-in-node","errorCode":null,"errorMessage":"Cannot get child at index \\`${index}\\` in node: ${Scrubber.stringify(\n          root\n        )}","messagePattern":"Cannot get child at index \\\\`(.+?)\\\\` in node: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/slate/src/interfaces/node.ts","lineNumber":283,"sourceCode":"      yield entry\n    }\n  },\n\n  child(root: Node, index: number): Descendant {\n    if (Node.isText(root)) {\n      throw new Error(\n        `Cannot get the child of a text node: ${Scrubber.stringify(root)}`\n      )\n    }\n\n    if (typeof index !== 'number') {\n      throw new Error('Expected index to be a number')\n    }\n\n    const c = root.children[index] as Descendant\n\n    if (c == null) {\n      throw new Error(\n        `Cannot get child at index \\`${index}\\` in node: ${Scrubber.stringify(\n          root\n        )}`\n      )\n    }\n\n    return c\n  },\n\n  *children(\n    root: Node,\n    path: Path,\n    options: NodeChildrenOptions = {}\n  ): Generator<NodeEntry<Descendant>, void, undefined> {\n    const { reverse = false } = options\n    const ancestor = Node.ancestor(root, path)\n    const { children } = ancestor\n    let index = reverse ? children.length - 1 : 0","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/ianstormtaylor/slate/blob/72a37c701e5da4bb13a305d416d9c070d19d3a45/packages/slate/src/interfaces/node.ts#L265-L301","documentation":"Node.child(root, index) throws when root.children[index] is undefined or null — i.e. the requested child index is out of bounds (negative or >= children.length). The message includes the offending index and a stringified copy of the root node to aid debugging.","triggerScenarios":"Calling Node.child(node, node.children.length) (classic off-by-one when iterating <= instead of <); using a stale index after children were removed; negative indices; assuming an element has children when it is empty.","commonSituations":"Loop bounds mistakes in manual traversal; cached paths/indices invalidated by concurrent edits; empty elements (children: []) accessed with index 0.","solutions":["Bounds-check first: 0 <= index < (node.children?.length ?? 0)","Fix loop conditions — use < children.length, not <=","Re-read the node or use Node.get/Editor.nodes with paths instead of cached indices after mutations"],"exampleFix":"// before\nfor (let i = 0; i <= node.children.length; i++) {\n  const c = Node.child(node, i) // throws on last iteration\n}\n\n// after\nfor (let i = 0; i < node.children.length; i++) {\n  const c = Node.child(node, i)\n}","handlingStrategy":"validation","validationCode":"if (\n  Number.isInteger(index) &&\n  index >= 0 &&\n  index < node.children.length\n) {\n  const c = Node.child(node, index)\n}","typeGuard":"const isIndexInBounds = (node, i) =>\n  Number.isInteger(i) && i >= 0 && i < node.children.length","tryCatchPattern":null,"preventionTips":["Use < not <= in child-iteration loops","Re-derive indices after mutations instead of caching them","Bounds-check before indexing into children arrays"],"tags":["slate","node-api","index-out-of-bounds","children","traversal"],"backgroundTag":"index-out-of-bounds","analyzedSha":"72a37c701e5da4bb13a305d416d9c070d19d3a45","analyzedAt":"2026-08-27T23:04:51.145Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}