{"record":{"id":"7b9f0aeae1bcc671","repo":"toeverything/AFFiNE","slug":"missingviewmodelerror","errorCode":"MissingViewModelError","errorMessage":"can not find element(id:${referenceElement})","messagePattern":"can not find element\\(id:(.+?)\\)","errorType":"exception","errorClass":"BlockSuiteError","httpStatus":null,"severity":"error","filePath":"blocksuite/affine/blocks/surface-ref/src/surface-ref-block.ts","lineNumber":308,"sourceCode":"    };\n    this.disposables.add(effect(refreshViewport));\n\n    const referenceId = this.model.props.reference;\n    const referenceXYWH$ = this._referenceXYWH$;\n    class SurfaceRefViewportWatcher extends LifeCycleWatcher {\n      static override readonly key = 'surface-ref-viewport-watcher';\n\n      private readonly _disposable = new DisposableGroup();\n\n      override mounted() {\n        const crud = this.std.get(EdgelessCRUDIdentifier);\n        const gfx = this.std.get(GfxControllerIdentifier);\n        const { surface, viewport } = gfx;\n        if (!surface) return;\n\n        const referenceElement = crud.getElementById(referenceId);\n        if (!referenceElement) {\n          throw new BlockSuiteError(\n            ErrorCode.MissingViewModelError,\n            `can not find element(id:${referenceElement})`\n          );\n        }\n        referenceXYWH$.value = referenceElement.xywh;\n\n        const { _disposable } = this;\n        refreshViewport();\n        _disposable.add(viewport.sizeUpdated.subscribe(refreshViewport));\n\n        if (referenceElement instanceof GfxBlockElementModel) {\n          _disposable.add(\n            referenceElement.xywh$.subscribe(xywh => {\n              referenceXYWH$.value = xywh;\n            })\n          );\n        } else if (referenceElement instanceof GfxPrimitiveElementModel) {\n          _disposable.add(","sourceCodeStart":290,"sourceCodeEnd":326,"githubUrl":"https://github.com/toeverything/AFFiNE/blob/26c515e050211269e911f7d9cfe162a26c83ed98/blocksuite/affine/blocks/surface-ref/src/surface-ref-block.ts#L290-L326","documentation":"Thrown by the surface-ref block's SurfaceRefViewportWatcher.mounted() when crud.getElementById(referenceId) returns null — i.e. the edgeless element referenced by the surface-ref block (a frame or a primitive) cannot be found in the current doc/surface. Error code MissingViewModelError. The block stores a reference id; on mount it tries to resolve that element to subscribe to its viewport/position changes.","triggerScenarios":"Rendering a surface-ref block whose model.props.reference points to an element id that does not exist: the referenced frame/shape was deleted, belongs to a different doc, or the surface is not loaded yet at mount time.","commonSituations":"Opening a doc where the referenced edgeless element was removed; cross-doc copy of a surface-ref block without its target; rendering the block before the surface/CRUD is ready; stale references after a migration that changed element ids.","solutions":["Before mounting/creating a surface-ref block, verify the referenced element exists via crud.getElementById(referenceId); if not, show a placeholder or remove the block.","When deleting edgeless elements, also clean up or mark surface-ref blocks that reference them.","If the element may load asynchronously, defer the throw: return early from mounted() when the element is missing instead of throwing (the watcher already returns early when !surface)."],"exampleFix":"// before\n// referenced element deleted -> mounted() throws MissingViewModelError\n\n// after (caller-side guard)\nconst ref = surfaceRefBlock.model.props.reference;\nif (!crud.getElementById(ref)) {\n  // render a 'missing reference' placeholder instead\n} else {\n  renderSurfaceRef(surfaceRefBlock);\n}","handlingStrategy":"validation","validationCode":"const crud = std.get(EdgelessCRUDIdentifier);\nif (!crud.getElementById(referenceId)) {\n  // render placeholder instead of mounting the surface-ref block\n}","typeGuard":"function referenceExists(crud, id) {\n  return !!crud.getElementById(id);\n}","tryCatchPattern":null,"preventionTips":["Verify the referenced element exists before mounting a surface-ref block.","When deleting edgeless elements, clean up surface-ref blocks that reference them.","Treat a missing reference as a render-time placeholder, not a throw."],"tags":["surface-ref","edgeless","missing-view-model","dangling-reference"],"backgroundTag":null,"analyzedSha":"26c515e050211269e911f7d9cfe162a26c83ed98","analyzedAt":"2026-08-12T13:15:16.447Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}