{"record":{"id":"24a6f0233876db0d","repo":"toeverything/AFFiNE","slug":"errorcode-missingviewmodelerror","errorCode":"ErrorCode.MissingViewModelError","errorMessage":"Cannot find block model for id ${this.blockId}","messagePattern":"Cannot find block model for id (.+?)","errorType":"exception","errorClass":"BlockSuiteError","httpStatus":null,"severity":"error","filePath":"blocksuite/framework/std/src/view/element/block-component.ts","lineNumber":114,"sourceCode":"    const expectedVersion = schema.version;\n    const actualVersion = this.model.version;\n    if (expectedVersion !== actualVersion) {\n      console.warn(\n        `Version mismatch for block ${this.model.id}, expected ${expectedVersion}, actual ${actualVersion}`\n      );\n      return true;\n    }\n\n    return false;\n  }\n\n  get model() {\n    if (this._model) {\n      return this._model;\n    }\n    const model = this.store.getModelById<Model>(this.blockId);\n    if (!model) {\n      throw new BlockSuiteError(\n        ErrorCode.MissingViewModelError,\n        `Cannot find block model for id ${this.blockId}`\n      );\n    }\n    this._model = model;\n    return model;\n  }\n\n  get parentComponent(): BlockComponent | null {\n    const parent = this.model.parent;\n    if (!parent) return null;\n    return this.std.view.getBlock(parent.id);\n  }\n\n  get renderChildren() {\n    return this.host.renderChildren.bind(this);\n  }\n","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/toeverything/AFFiNE/blob/b4c8548c09da21b2898443559a5b846f0ccf5dd8/blocksuite/framework/std/src/view/element/block-component.ts#L96-L132","documentation":"BlockComponent.model getter resolves the component's blockId through store.getModelById(); if the doc no longer contains a model with that id it throws MissingViewModelError instead of returning undefined. This happens when a view component outlives its model — the block was deleted from the yjs doc but the custom element is still mounted or being read.","triggerScenarios":"Reading this.model (directly or via parentComponent/childComponent/render) on a BlockComponent whose blockId has no model in the store: block deleted while its element was still connected, a stale element retained after a view re-render, or a component created with a wrong/unset blockId.","commonSituations":"Deleting blocks programmatically while their components are mid-lifecycle; undo/redo replacing a model id; custom code holding references to block components past deletion; snapshot import that doesn't map ids consistently.","solutions":["Check existence before use: const model = doc.getModelById(component.blockId); if (!model) skip teardown logic.","Do removals through the standard APIs (doc.deleteBlock) so views unmount with the model; let the view registry drive element lifetime.","Never cache BlockComponent instances across mutations — re-query via std.view.getBlock(id).","In custom components, avoid touching this.model after disconnectedCallback."],"exampleFix":"// before\nconst comp = std.view.getBlock(id);\nconst flavour = comp.model.flavour; // throws if model deleted\n\n// after\nconst comp = std.view.getBlock(id);\nconst model = comp ? doc.getModelById(comp.blockId) : null;\nif (!model) return;\nconst flavour = model.flavour;","handlingStrategy":"type-guard","validationCode":"const model = doc.getModelById(component.blockId);\nif (model) {\n  // safe to read component.model / render\n}","typeGuard":"function blockModelExists(doc: Store, blockId: string): boolean {\n  return doc.getModelById(blockId) != null;\n}","tryCatchPattern":null,"preventionTips":["Delete blocks via doc.deleteBlock so views unmount with their models.","Re-query components through std.view.getBlock(id) instead of caching element references.","Never read this.model after disconnectedCallback or outside the render lifecycle."],"tags":["blocksuite","block-model","lifecycle","stale-reference","doc-mutation"],"backgroundTag":"stale-view-model-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"}