What causes the markerArray to be undefined in this context? The concept of "this" explained

I am trying to grasp the concept of 'this'. In a specific scenario, I keep encountering the error message "this.markerArray is undefined". However, I have defined markerArray as a global variable so I am puzzled by this issue.

@Component({
  selector: 'app-open-street-map',
  templateUrl: './open-street-map.component.html',
  styleUrls: ['./open-street-map.component.css']
})
export class OpenStreetMapComponent implements OnInit {
  @Output() private add = new EventEmitter();
  @Output() private edit = new EventEmitter<number>();
  artworkList: Artwork[];
  map;
  //declaration made here
  markerArray = [];

  constructor() { }

  ngOnInit() {
  });

  buildMarkers(artworkList) {
    for (let artwork of artworkList) {
      const marker = this.buildPopup(artwork);
      this.markers.push(marker);
    }
    console.log("marker", marker);
    console.log("markerArray", this.markerArray);
  }
}

I have excluded a significant amount of code from this TypeScript page! The objective is to populate the markerArray. While I acknowledged your advice on initializing it, I have done so, but all array inputs still show as "undefined"?!

Answer №1

Make sure to start off by setting it up with a blank array

  newArray = [];

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

Guide to resolving typescript issue 'name' is not found in type 'ByRoleOptions' while accessing by name using getByRole in react-testing-library

I have a custom component that showcases a collection of filters in the form of removable chips. To test this functionality, I am utilizing react-testing-library with a focus on querying by accessible name as outlined here, using the getByRole method. The ...

Issue Encountered with @Input() in TypeScript

I'm currently working on developing an app with Angular 4 and encountered an error message when using @Input('inputProducts') products: Product[]; The specific error I received reads: [tslint] In the class "ProductListComponent", the di ...

Exploring the Power of Functions within Angular 4 Subscriptions

I am encountering an issue when trying to call a function within a subscription. The problem arises in this function where I make a call to a subscription that refreshes a token. If the returned value is "true", I then attempt to call the same function aga ...

Error encountered with custom iterable in TypeScript when using the for...of loop

Attempting to create a custom iterable in my Angular application has resulted in an error message: "Type 'Connection' is not an array type or a string type." This occurs when trying to loop through the class using a for..of statement. Further in ...

Using Typescript and React to render `<span>Text</span>` will only display the text content and not the actual HTML element

My function is a simple one that splits a string and places it inside a styled span in the middle. Here's how it works: splitAndApplyStyledContent(content: string, textType: string, separator: string) { const splittedContent = content.split(separat ...

Import reactjs modules without the need for Browserify, Webpack, or Babel

I am attempting to set up a TypeScript HTML application in Visual Studio. My goal is to incorporate reactjs v0.14.7 without relying on tools like Browserify. But, how can I utilize the react-dom module in this scenario? Let's set aside TypeScript fo ...

How to conceal a column in an Angular Material table

Here is my Angular 2 material table setup: <mat-table #table [dataSource]="dataSource" > <!--- Note that these columns can be defined in any order. The actual rendered columns are set as a property on the row definition" -- ...

The Angular2 application is experiencing technical difficulties

Hello, I am encountering an issue with my small Angular (2.1.1) application that consists of a module and a component. Everything was functioning correctly until this error occurred: error_handler.js:53 TypeError: jit_selectOrCreateRenderHostElement5 is n ...

Tips for using the validation method created for the mat datepicker in different components

Within my component, I have implemented two date pickers with start and end date validation. This function is responsible for checking if the start date precedes the end date, and it should be applied to all date pickers. Is there a way to avoid duplicatin ...

Error encountered with default theme styling in Material-UI React TypeScript components

Currently, I am working on integrating Material-UI (v4.4.3) with a React (v16.9.2) TypeScript (v3.6.3) website. Following the example of the AppBar component from https://material-ui.com/components/app-bar/ and the TypeScript Guide https://material-ui.com/ ...

NG build error: Module parsing failed due to an unexpected token - no updates made

Two days ago, out of nowhere, we started encountering build errors during deployment using GitLab CI. No alterations have been made to the build scripts, and none of the versions of NPM, NG, or Angular have been modified. The same compilation commands cont ...

Include a log out option in the side menu of the ionic2 application

I am working with the sidemenu template to kick off my application. I aim to incorporate a button within the sidemenu that enables users to trigger a modal alert for confirming logout. Below is the code snippet: app.component.ts: import { Component, View ...

What is the ideal configuration for Typescript within ASP.NET 4 MVC 5 on Visual Studio 2015?

Currently, I am in the process of integrating a TypeScript project into a VS2015 MVC 5 project (which is based on ASP.NET 4, specifically not asp.net 5 or asp.net 6 - only the MVC portion is version 5). All aspects of my query pertain solely to this target ...

Determining the output type of a function that yields a component on-the-fly

I am currently working on removing the "any" in the return type of the getFilterComponent function: import { type default as React } from "react"; type filterType = 'filterA' | 'filterC' | 'filterB' interface Filte ...

Setting Angular 2+ FormGroup value in a child component form using data passed from the parent

As a beginner in coding, I encountered an issue with sharing a form component between an add member and edit member component. While copying the form code into each component works fine, it goes against the DRY principle and could lead to banning from furt ...

Angular 12 - Encountering an issue with undefined properties when trying to access 'values'

Currently in the process of upgrading Angular Version from 8 to 12. Successfully made the upgrade from 8 to 11 without any issues. However, upon updating Angular to version 12, encountered an error stating "loadComponent TypeError: Cannot read propert ...

Developing with Angular and Firebase: Setting up a new data node

Is there a way to create a new node called terriangen, add a key, and set the object data in Firebase? -usernames -{UID} -mylibrary -{key} -terriangen -{key} type:mountain name:1.png This is the ...

Obtain the user's ID in string format from the clerk

I'm trying to retrieve the clerk ID to cross-reference with the backend. The issue is that the clerk is using a hook which isn't compatible with this function type. export const getServerSideProps = async ({ req }) => { const { isLoaded, is ...

Connecting the SignalR client to various servers

I am currently incorporating SignalR version 2.x into my ASP.Net MVC application, which is also using the same version of SignalR in my Angular client app. The ASP.Net MVC application is hosted at http://localhost:42080, while the Angular app is hosted at ...

Exploring the canDeactivateFn syntax with Angular Documentation

As a first-year university student, I recently discovered that the canDeactivate() guard in Angular is deprecated, while the canDeactivateFn guard functions without any issues. Admittedly, navigating through official documentation is still new to me. From ...