{"record":{"id":"8ade845278c2bd83","repo":"toeverything/AFFiNE","slug":"errorcode-norootmodelerror","errorCode":"ErrorCode.NoRootModelError","errorMessage":"This doc is missing root block. Please initialize the default block structure before connecting the editor to DOM.","messagePattern":"This doc is missing root block\\. Please initialize the default block structure before connecting the editor to DOM\\.","errorType":"exception","errorClass":"BlockSuiteError","httpStatus":null,"severity":"critical","filePath":"blocksuite/framework/std/src/view/element/lit-host.ts","lineNumber":131,"sourceCode":"  }\n\n  get range(): RangeManager {\n    return this.std.range;\n  }\n\n  get selection(): StoreSelectionExtension {\n    return this.std.selection;\n  }\n\n  get view(): ViewStore {\n    return this.std.view;\n  }\n\n  override connectedCallback() {\n    super.connectedCallback();\n\n    if (!this.store.root) {\n      throw new BlockSuiteError(\n        ErrorCode.NoRootModelError,\n        'This doc is missing root block. Please initialize the default block structure before connecting the editor to DOM.'\n      );\n    }\n\n    this.std.mount();\n    this.tabIndex = 0;\n  }\n\n  override disconnectedCallback() {\n    super.disconnectedCallback();\n    this.std.unmount();\n  }\n\n  override async getUpdateComplete(): Promise<boolean> {\n    try {\n      const result = await super.getUpdateComplete();\n      const rootModel = this.store.root;","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/toeverything/AFFiNE/blob/b4c8548c09da21b2898443559a5b846f0ccf5dd8/blocksuite/framework/std/src/view/element/lit-host.ts#L113-L149","documentation":"LitHost.connectedCallback (the <editor-host> root) requires store.root to exist before std.mount(); a doc with no root block has nothing to render, so BlockSuite fails fast with NoRootModelError. It tells you the document's block tree was not initialized (no root flavour like affine:page was ever added) before the editor was attached to the DOM.","triggerScenarios":"Creating a new empty doc (new Doc(...)) and appending the editor host before any root block exists; forgetting doc.load()/init with a default structure; importing a snapshot that failed or produced no root; referencing a doc id that has no data yet.","commonSituations":"Bootstrapping fresh workspaces without calling the standard init (e.g. doc.load(() => doc.addBlock('affine:page', {}, ...))); lazy-loading docs where the editor mounts before the (async) doc content arrives; snapshot middleware skipping the root node.","solutions":["Initialize the default block structure before connecting: use doc.load(init => init.block('affine:page', ...)) or add the page/root block, then mount the editor.","For lazily loaded docs, wait for the doc content (workspace.doc loaded / doc.loaded await) before appending <editor-host>.","Verify snapshot import actually created a root (check doc.root) and that the root flavour's schema is registered.","Show a loading state until store.root is non-null instead of mounting early."],"exampleFix":"// before\nconst doc = workspace.createDoc(docId);\ndocument.body.append(editor); // NoRootModelError\n\n// after\nconst doc = workspace.createDoc(docId);\ndoc.load(init => init.block('affine:page', {}, init.block('affine:surface', {})))\n  .then(() => document.body.append(editor));","handlingStrategy":"validation","validationCode":"// Only mount the editor when the doc has a root:\nif (doc.root) {\n  document.body.append(editor);\n} else {\n  await doc.load(init => init.block('affine:page', {}));\n  document.body.append(editor);\n}","typeGuard":"function docHasRoot(doc: Store): boolean {\n  return doc.root != null;\n}","tryCatchPattern":null,"preventionTips":["Initialize the default block structure (root page block) before connecting the editor host to DOM.","Await doc.load()/doc.loaded for lazily fetched docs before mounting.","Register the root flavour's schema; a missing schema can also leave root unresolved."],"tags":["blocksuite","doc-init","root-block","editor-mount","lifecycle"],"backgroundTag":"document-root-missing","analyzedSha":"b4c8548c09da21b2898443559a5b846f0ccf5dd8","analyzedAt":"2026-08-18T21:16:52.546Z","contentChangedAt":"2026-08-18T21:16:52.546Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}