An error occurred: Unable to access the 'basemapLayer' property due to it being undefined

Oops! TypeError: Unable to access 'basemapLayer' property of undefined

I recently put together a simple application using the Angular CLI. After successfully building and running the application with ng serve -o, I encountered an issue in Chrome where the map section failed to load. Upon inspecting the page, I came across this error message in the console:

ERROR TypeError: Cannot read property 'basemapLayer' of undefined

Configuration

  • Angular 4
  • Chrome 61
  • leaflet 1.2.0
  • esri-leaflet 2.1.1
  • types/leaflet for 1.2
  • types/esri-leaflet for 2.1.

Steps to Recreate the Problem:

If you already have angular CLI installed, follow these steps:

Perform Steps 1-6 & 10 in your terminal/cmd prompt window

  1. Create a new application ng new esriLeafletApp
  2. Navigate to the new application cd esriLeafletApp
  3. npm install --save leaflet
  4. npm install --save esri-leaflet
  5. npm install --save @types/esri-leaflet
  6. npm install --save @types/leaflet
  7. Update the contents of the app.component.ts file

import { Component, OnInit } from '@angular/core';

import * as L from 'leaflet';
import 'esri-leaflet';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit {
  title = 'app';

  constructor() { }

  ngOnInit () {
    const map = L.map('map').setView([51.505, -0.09], 13);
    L.esri.basemapLayer('Streets').addTo(map);
  }
}

  1. Update the contents of the app.component.html file

<div style="text-align:center">
    <h1>
       Welcome to {{title}}!
    </h1>
</div>
<div id="map"></div>

  1. Update the contents of the app.component.css file

    #map {
      height: 500px;
      width: 100%;
    }

  2. Build and run the application ng serve -o

  3. Check the application in Chrome
  4. Inspect the code and locate the error in the console

We Need Your Assistance

Can anyone explain why esri is coming up as undefined in the line

L.esri.basemapLayer('Streets').addTo(map);
and offer suggestions on how to address it?

Answer №1

It appears that the problem is related to the typings file for esri-leaflet (@types/esri-leaflet), rather than with esri-leaflet itself. I have submitted an issue on the DefinitelyTyped GitHub repository.


A possible solution:

  • Delete ESRI typings from package.json and node_modules
  • Import esri using:
    import * as esri from 'esri-leaflet';
  • Utilize esri directly instead of as a Leaflet extension
  • You can still use leaflet typings without any issues

import { Component, OnInit } from '@angular/core';

import * as L from 'leaflet';
import * as esri from 'esri-leaflet';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit {
  title = 'app';

  constructor() { }

  ngOnInit () {
    const map = L.map('map', {
      maxZoom: 18,
      minZoom: 0
    }).setView([51.505, -0.09], 15);

    const esriLayer = esri.basemapLayer('Imagery');
    map.addLayer(esriLayer);
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize the optional chaining feature when accessing properties that may be optional

I recently encountered an issue in my Typescript project where accessing properties or functions of optional properties did not throw any errors. Here is an example: type Example = { bar?: string[] } const foo: Example = {} // Although no error occu ...

Encountering an "ionic 4 frame-ancestors *" error while attempting to watch a Twitter video

Currently, I am in the process of developing a news app using Ionic 4. I recently tackled the challenge of embedding tweets in Twitter cards successfully. However, a new issue has arisen. When a tweet includes a Youtube video, everything works perfectly ac ...

Is there a way to efficiently execute an API function for every element within an array in a sequential manner?

I am currently facing a challenging problem while working with Angular and RxJs. I have an array containing several IDs: ids = [1,2,3,4] There is an API that can be called with a specific ID parameter to delete the corresponding item from the database: th ...

Angular 12: TypeScript Issue TS2339 - Unable to Locate Property on Type

Whenever I use the code below, I encounter error TS2339: Property 'timestamp' does not exist on type 'LogRepair[]' In the component's HTML file, I am attempting to loop through an array of properties defined in the LogRepair typ ...

Create a versatile generic object using TypeScript

Looking to create a versatile onFilterChange helper function that works for all filters, eliminating the need to write it out separately each time. However, I've hit a snag: // helper.ts export function onFilterChange(prevState: Record<string, any& ...

One way to update the value of the current array or object using ngModel in Angular 2 is to directly

I have a situation where I am dealing with both an array and an object. The array is populated with data retrieved from a service, while the object contains the first element of that array. feesEntries: Array<any> = []; selectedFeesEntry: any; clien ...

Tips for utilizing the forEach method in Angular 2 without relying on ngFor?

I recently started learning Angular 2 and I am trying to figure out how to access array details using a forEach loop and apply certain conditions on it. Once I make the necessary changes, I want to display this data using ngFor. In Angular 1, this was ea ...

What is the process for inheriting a parent component's template in Angular 4?

Is there a way to inherit the template from a parent component in Angular 4 without overriding it completely? Many tutorials show how to override the parent component's template like this: import { Component } from '@angular/core'; import { ...

What is the method for identifying the corresponding value that should be linked to the remaining select based on the selected option?

Is it possible for the first select to dynamically affect the value of another select? <form [formGroup]="myForm"> <div class="modal-body"> <p><strong>Rate:</strong></p> {{ApplesPerCash}} <p><st ...

How to customize Material UI Autocomplete options background color

Is there a way to change the background color of the dropdown options in my Material UI Autocomplete component? I've checked out some resources, but they only explain how to use the renderOption prop to modify the option text itself, resulting in a a ...

Verify if a given string exists within a defined ENUM in typescript

I have an enum called "Languages" with different language codes such as nl, fr, en, and de. export enum Languages { nl = 1, fr = 2, en = 3, de = 4 } Additionally, I have a constant variable named "language" assigned the value 'de'. My g ...

Dynamic routing with ngIf in Angular 2's router system

Is there a way to use *ngIf with dynamic router in Angular? Let's say I have a top navigation component with a back button, and I only want the back button to be visible on the route 'item/:id'. I tried using *ngIf="router.url == '/ite ...

The NullInjectorError is thrown when the Angular service providedIn: root is imported from a library

After moving my service into a separate npm package, I encountered an issue where the service was marked to be provided in the root injector but resulted in a NullInjectorError when trying to use it in my app. To solve this problem, I had to include Quer ...

Tips for accessing HttpParams within a WebApi Controller while utilizing the [HttpPut] method

I am attempting to update a specific resource by accessing it through the PUT method in an Angular service. RollBackBatchById(selectedBatchId: number) { const params = new HttpParams(); params.append('resourceId', resourceId.toString()); ...

Angular 5 does not recognize the function submitEl.triggerEventHandler, resulting in an error

Greetings! I am currently working on writing unit test cases in Angular5 Jasmine related to submitting a form. Below is the structure of my form: <form *ngIf="formResetToggle" class="form-horizontal" name="scopesEditorForm" #f="ngForm" novalidate (ngSu ...

Using nginx reverse proxy to serve an Angular application hosted on a nginx server

My angular app is running in a docker container on nginx named "website". I am trying to access this container from another nginx reverse-proxy container called "test.nginx", but I'm unsure how to configure the nginx.conf for reverse-proxy. First, I ...

Leverage rxjs/Typescript to transform an array nested within the data received from an external API

Exploring Typescript/Javascript is a new adventure for me, especially as I delve into the world of rxjs. Here's a snippet of code that I've been working with: return this.http.get<IXenoCantoResponse>(`${this.corsAnywhereUrl}${this.xenoCant ...

Is tsconfig.json necessary for JS libraries without TypeScript to include a .d.ts file when shipping?

I am currently in the process of creating a .d.ts file for an established JavaScript library that does not utilize the TypeScript compiler or include any TypeScript code. Should I include a tsconfig.json file in the library to ensure proper interpretation ...

Struggling to connect the array of objects from the .ts file with the template (.html) in Angular

Inside this .ts file, I am populating the "mesMenus" array that I want to display in the .html file: export class MenusComponent{ mesMenus= new Array<Menu>(); constructor(private gMenuService:GestionMenuService){ this.gMenuService.onAdd ...

What is the best way to execute TypeScript programs on an Android device?

Is there a way to execute TypeScript programs on an Android phone? Are there any offline apps specifically designed for running TypeScript programs on Android devices? ...