Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mapInstance is undefined while trying to remove #142

Closed
mloffer opened this issue May 10, 2019 · 1 comment
Closed

mapInstance is undefined while trying to remove #142

mloffer opened this issue May 10, 2019 · 1 comment

Comments

@mloffer
Copy link
Contributor

mloffer commented May 10, 2019

While quickly switching between two mgl-map instances, a hook to destroy the instance is scheduled, but the reference is undefined when called.

app.component.html:

<div [ngSwitch]="whichMap">
	<mgl-map *ngSwitchCase="'first'"></mgl-map>
	<mgl-map *ngSwitchCase="'second'"></mgl-map>
</div>

app.component.ts:

export class AppComponent implements OnInit {
	whichMap = 'first';

	ngOnInit() {
		// Change which map we're displaying after the current synchronous code runs
		Promise.resolve(null).then(() => this.whichMap = 'second');
	}
}
ERROR TypeError: Cannot read property 'remove' of undefined
    at MapService.push../node_modules/ngx-mapbox-gl/fesm5/ngx-mapbox-gl.js.MapService.destroyMap (ngx-mapbox-gl.js:83)
    at MapComponent.push../node_modules/ngx-mapbox-gl/fesm5/ngx-mapbox-gl.js.MapComponent.ngOnDestroy (ngx-mapbox-gl.js:3324)

Only one MapComponent constructor gets run in this scenario, so I'm unclear on why MapComponent.ngOnDestroy gets called at all.

This seems to be resolved by performing a safety check before attempting to call mapInstance.remove(). See pull request #141.

@Wykks
Copy link
Owner

Wykks commented May 20, 2019

Yep indeed. ngOnDestroy can be called, even if ngOnInit hasn't been called 👍

@Wykks Wykks closed this as completed May 20, 2019
Wykks added a commit that referenced this issue May 20, 2019
fix(map): Add safety check for MapService.mapInstance before attempting to remove (close #142)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants