{"record":{"id":"c7036460364edda3","repo":"angular/components","slug":"cannot-interact-with-a-markerclusterer-before-it-h","errorCode":null,"errorMessage":"Cannot interact with a MarkerClusterer before it has been initialized. Please wait for the MarkerClusterer to load before trying to interact with it.","messagePattern":"Cannot interact with a MarkerClusterer before it has been initialized\\. Please wait for the MarkerClusterer to load before trying to interact with it\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/google-maps/deprecated-map-marker-clusterer/deprecated-map-marker-clusterer.ts","lineNumber":525,"sourceCode":"            this.markerClusterer.repaint();\n            for (const marker of markersToRemove) {\n              this._currentMarkers.delete(marker);\n            }\n          });\n        });\n      });\n  }\n\n  private _getInternalMarkers(\n    markers: MapMarker[] | QueryList<MapMarker>,\n  ): Promise<google.maps.Marker[]> {\n    return Promise.all(markers.map(markerComponent => markerComponent._resolveMarker()));\n  }\n\n  private _assertInitialized(): asserts this is {markerClusterer: MarkerClustererInstance} {\n    if (typeof ngDevMode === 'undefined' || ngDevMode) {\n      if (!this.markerClusterer) {\n        throw Error(\n          'Cannot interact with a MarkerClusterer before it has been initialized. ' +\n            'Please wait for the MarkerClusterer to load before trying to interact with it.',\n        );\n      }\n    }\n  }\n}\n","sourceCodeStart":507,"sourceCodeEnd":533,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/google-maps/deprecated-map-marker-clusterer/deprecated-map-marker-clusterer.ts#L507-L533","documentation":"DeprecatedMapMarkerClusterer stores the MarkerClustererInstance asynchronously after the map resolves. Any public API (fitMapToMarkers, getters/setters, etc.) funnels through _assertInitialized, which throws if markerClusterer is still undefined. It guards against touching the cluster before its async construction finished.","triggerScenarios":"Calling fitMapToMarkers, get/set averageCenter, batchSizeIE, calculator, clusterClass, etc. immediately after creating the component, before _resolveMap().then(...) has assigned this.markerClusterer.","commonSituations":"Calling cluster APIs in ngOnInit/ngAfterViewInit of a parent before the promise resolves; interacting in tests without awaiting initialization; slow Google Maps script loading delaying map resolution.","solutions":["Wait for the map/cluster to load before interacting — e.g. await mapComponent._resolveMap() first.","Defer API calls until after a tick/subscription indicating the cluster is ready.","In tests, await a stable state (e.g. whenStable or a promise) before assertions.","Migrate to @googlemaps/markerclusterer which exposes clearer lifecycle APIs."],"exampleFix":"// before\nngAfterViewInit() { this.cluster.fitMapToMarkers(); }\n// after\nasync ngAfterViewInit() {\n  await this.map._resolveMap();\n  this.cluster.fitMapToMarkers();\n}","handlingStrategy":"try-catch","validationCode":"async function whenClusterReady(cluster: any) {\n  await cluster._resolveMap?.();\n  if (!cluster.markerClusterer) throw new Error('MarkerClusterer not ready yet');\n}","typeGuard":"function isClusterReady(c: any): c is {markerClusterer: NonNullable<typeof c.markerClusterer>} {\n  return c?.markerClusterer != null;\n}","tryCatchPattern":"try {\n  this.cluster.fitMapToMarkers();\n} catch (e) {\n  if (String((e as Error).message).includes('before it has been initialized')) {\n    this.cluster._resolveMap().then(() => this.cluster.fitMapToMarkers());\n  }\n}","preventionTips":["Gate all cluster API calls behind the map's resolve promise.","Never call cluster methods synchronously in ngOnInit/ngAfterViewInit.","In tests, await fixture.whenStable() or the initialization promise.","Prefer declarative inputs over imperative calls before init."],"tags":["angular","google-maps","async-initialization","lifecycle","dev-mode"],"backgroundTag":"not-initialized-yet","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}