Issue with Undefined Variable in Angular 2 and Ionic Framework

I included the following code in my HTML file:

<ion-col col-3 align="right">
          <ion-item>
            <ion-label>Show as</ion-label>
            <ion-select [ngModel]="SelectedView" (ngModelChange)="onViewChange($event)>
              <ion-option value="List">List</ion-option>
              <ion-option value="Map">Map</ion-option>
            </ion-select>
          </ion-item>
        </ion-col>
    

And in my customer.ts file, I have the following code:

export class customerpage implements OnInit {

          constructor(public modalCtrl: ModalController,
            public popoverCtrl: PopoverController,
            private _customerservice: CustomersService,
            public navCtrl: NavController,
            public navParams: NavParams) {
    
            this.ListPositions = [];
            this.customersArray = [];
            this.customersView = [];
          }
    
          ngOnInit() {
    
            this.SelectedCategory = "";
    
            this.customersArray = [];
            this.customersView = [];
    
            setTimeout(() => {
    
              this.GetCustomers();
            }, 500);
    
          }
    
          GetCustomers() {
            const arrayofroles$ = this._customerservice.GetAllCustomers();
            arrayofroles$.subscribe(res => {
              this.customersArray = res;
              this.customersView = this.customersArray;
    
            },
              err => { },
              () => {
    
                this.loadingCustomers = false;
              });
    
          }
    
          ionViewDidLoad(): void {     
    
          }
    
          onViewChange(event: any, Customers: any) {
    
            this.initMap(Customers);
    
          }
    
          initMap(Customers: any) {
    
              this.customersView; // UNDEFINED!
          }
    
    }
    

Initially, during ngInit, I load the customersview from the database. When onViewChange is triggered from the HTML, I call the initMap function and try to access customersView, but it returns undefined for my local variable.

Answer №1

A potential solution may exist: It appears that there is an issue with data synchronization.

// retrieve subscription using this function
fetchClients() {
        const clientSubscription = this._clientservice.getAllClients();
        return clientSubscription;
}


onViewUpdate(event: any) {
         this.fetchClients()
         .subscribe((clients: any) => {
             this.initializeMap(clients);
         }); 
}

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

Controlling the upper and lower limits in an input field for numerical values while manually typing in the text

Trying to implement an Angular component input with number type that allows setting a maximum and minimum value. Here is the code snippet used for calling the component in HTML: <app-input-number [(value)]="inputMinMaxValueNumber" [min]="min" [max]="m ...

What is the best way to pass a state within a route component in react-router?

... import { useNavigate, NavigateFunction } from "react-router"; ... function Form(): JSX.Element { const navigateToCountry = (country: string) => { // Code to navigate to country page with the given country } const [selectedCount ...

Add a component to another component in real-time

Check out my StackBlitz demo: DEMO I'm attempting to insert the 'table' component into the #test section of the app component when the visualization type is 'table'. To achieve this, I am using the createTable() function which gen ...

Incorporating a new attribute into the JQueryStatic interface

I am trying to enhance the JQueryStatic interface by adding a new property called someString, which I intend to access using $.someString. Within my index.ts file, I have defined the following code: interface JQueryStatic { someString: string; } $.s ...

Configuring IIS routing for seamless integration with Angular routing

After deploying my front-end Angular application on an IIS server and my back-end ASP.NET web API application on a separate server, I encountered an issue. Even though I can access the web page when visiting the Angular application's URL and navigate ...

Attempting to start a fresh Angular project using the command line, only to be met with an unexpected error message

⠹ Installing packages (npm)...npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41202f263101716f716f71">[email ...

Nested ng-template in PrimeNG dropdown menu allows for a more dynamic and

I'm having trouble figuring out how to implement nested ng-template in Angular 5.2. Currently, I have a component within my app that utilizes PrimeNG dropdown: @Component({ selector: 'app-dropdown', template: ` <p-dropdown [opti ...

Performing an HTTP POST request in Angular 2

After starting my work with Angular 2 and TypeScript, everything was going great. However, I encountered an issue when working with a REST API (POST) where the console log displayed Response {_body: "", status: 204, statusText: "Ok", headers: Headers, type ...

Quirky happenings in Typescript

TS Playground function foo(a: number, b: number) { return a + b; } type Foo1 = typeof foo extends (...args: unknown[]) => unknown ? true : false; // false type Foo2 = typeof foo extends (...args: any[]) => unknown ? true : false; // true What is ...

Harness the power of TypeScript in a single test file with jest's expect.extend() method

This question is similar to Can you limit the scope of a TypeScript global type? but it presents a slightly different scenario that requires clarification (although an answer to this would be greatly appreciated as well). In my Jest setup, I am attempting ...

Struggling with transitioning from TypeScript to React when implementing react-data-grid 7.0.0

I'm trying to add drag and drop functionality to my React project using react-data-grid, but I keep encountering a "TypeError: Object(...) is not a function" error. I have a TypeScript version of the file in the sandbox as a reference, but when I try ...

Convert a TypeScript array of strings to a boolean array: a step-by-step guide

Upon receiving a list of objects from the front end as: item=["false","true"] I proceed to check a column within my records to identify values containing "true" or "false" using the following code: this.records.filter(x=> items.includes(x.column)) Unf ...

Create a dynamic Prisma data call by using the syntax: this.prisma['dataType'].count()

I'm currently working on implementing a counting function that can be utilized in all of my objects. In my 'core' file, Prisma is involved in this process. This allows me to execute commands like this.user.count() or this.company.count() I ...

The type '(props: Props) => Element' cannot be assigned to the type 'FunctionComponent<FieldRenderProps<any, HTMLElement>>' in React-final-form

I'm fairly new to using TypeScript, and I am currently working on developing a signUp form with the help of React-Final-Form along with TypeScript. Here is the code snippet that describes my form: import React from "react"; import Button from "@mater ...

Ways to display a popup when hovering over a marker in ngx-mapbox-gl

I have a current implementation that displays markers and popups on click of markers. We now need to update it to show popups on hover instead. Here is the structure of my template: <mgl-map #map1 [style]="'mapbox://styles/mapbox/streets ...

Ways to update the component's state externally

I'm new to Next.js (and React) and I'm attempting to update the state of a component from outside the component. Essentially, I am conditionally rendering HTML in the component and have a button inside the component that triggers a function to se ...

Importing SCSS files dynamically with TypeScript

Recently, I utilized Create React App (CRA) to create a new project and then included node-sass in order to import SCSS files. An example: import "./App.scss"; Although this method works without any issues, I encountered a problem when trying t ...

Creating a String Array and linking it to an Input Field

I'm currently working on a code that involves mapping through an array of strings using observables. My objective is to display the value from this array inside an input field. However, despite being able to view the array in the console, I encountere ...

Need help in NestJS with returning a buffer to a streamable file? I encountered an error stating that a string is not assignable to a buffer parameter. Can anyone provide guidance on resolving this issue?

The issue description: I am having trouble returning a StreamableFile from a buffer. I have attempted to use buffer.from, but it does not seem to work, resulting in the error message below. Concern in French language: Aucune surcharge ne correspond à cet ...

When executing `npm run e2e`, the terminal does not display any output

After building my application with Angular 7, here is a snippet from my package.json file: "scripts": { "ng": "ng", "standardize": "prettier **/*.ts --write", "start": "npm run standardize && ng serve --port 5000", "build": "npm run standa ...