Undefined Typescript variable within a nested function

I encountered an issue where I am attempting to access an outer variable within a nested function, but my debugger keeps showing the variable as undefined:

export class TestClass {
  someObj = [ { id=1 }];

  changeData() {
    const someId = 1;
    const test = {
       attr: function() { return (this.somObj.find(x => x.id === someId ));   }
    };
  }
}

The problem is that this.someObj is coming up as undefined. Is there a way to solve this?

Answer №1

You made a spelling error in this.somObj.

To correct it, you need to reference this with a different variable first:

 changeData() {
    const someId = 1;
    const that = this;
    const test = {
       attr: function() { return (that.someObj.find(x => x.id === someId ));   }
    };
  }

Another suggestion provided by someone is to use an arrow function:

attr: () => { return (this.someObj.find(x => x.id === someId )); }

Answer №2

myObject = [ { num=5 }];

maybe you intended to write:

myObject = [ { num:5 }];

?

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

Validating minimum and maximum values with Angular 2 FormBuilder

I am currently developing a form using Angular 2 Formbuilder and I want to ensure that users can only input positive values into the amount field (with a minValue of 0 and maxValue of 100). How can I go about implementing minimum and maximum value validati ...

Angular 'nativeElement' is not defined

I'm currently working with Angular 11.0.1 and Ionic 5.4.16, attempting to integrate a Google map into my project. I have added the necessary API key and executed the commands below: map.page.html <div #mapElement class="map"> map.pa ...

exploring asynchronous javascript behavior utilizing the sleep/setTimeout function in the context of THREEJS and Angular

As a newcomer to Javascript and asynchronous programming, I am facing what I believe to be a beginner's issue. I have been using ThreeJS to create a scene with multiple objects (approximately 100) and everything was working smoothly until now. My cu ...

Executing an animation in Angular 4 using a Directive

There's an ongoing issue on the repository here, but I wanted to see if anyone here could help as well. I am trying to programmatically trigger an animation from a Directive. However, when using Renderer.animate, I receive the following error: Rende ...

Find the appropriate return type for a TypeScript function based on its argument

Is it feasible in TypeScript to infer the return type of a function based on its arguments? This feature would be beneficial when extracting specific properties from, for example, a database query. Here is an illustration (https://repl.it/repls/Irresponsi ...

Are ngFormModel characteristics subject to change?

I've been facing challenges while working with ngFormModel and dynamic properties from classes. Despite my efforts, I'm unable to update the ngFormModel when changing variables to reflect new values. You can see an example of this issue in the fo ...

Closing iframe in Angular 4 after redirection

I am currently working on an Angular 4 application that involves integrating a third-party payment system called Tranzila. Tranzila offers a convenient integration method using an iframe, allowing users to make payments on their domain without me having to ...

Uploading Images to Imgur with Angular 4

As a newcomer to TypeScript, I am faced with the challenge of uploading an image to the Imgur API using Angular. Currently, my approach involves retrieving the file from a file picker using the following code: let eventObj: MSInputMethodContext = <MSIn ...

angular component communication between neighboring elements

Hello everyone, I am looking for guidance on how to facilitate interaction between adjacent components in Angular that are not parent/child. For instance, imagine two components that are neighbors - Component A and Component B. In Component A, there is a b ...

invoking object-oriented PHP method through AJAX

I have a PHP file with OOP and I want to call one of the functions using AJAX. I have done a lot of research on this (mostly on Stack Overflow), but for some reason, it's not working. I can confirm that the AJAX function is being called (I added an a ...

An assortment of diverse data types in TypeScript arrays

Considering adding type to my existing API, I have a function that can accept a string, function, or object (utilizing overloading). However, it is also able to accept an array of mixed values. Is there a way to have an Array in TypeScript that consists o ...

Incorporating a Custom CKEditor5 Build into an Angular Application

I am currently in the process of developing an article editor, utilizing the Angular Integration for CKEditor5. By following the provided documentation, I have successfully implemented the ClassicEditor build with the ckeditor component. Below are the ess ...

The intricate nature of a generic asynchronous return type hinders the ability to accurately deduce precise

My goal in this coding playground is to create a versatile API handler helper that guarantees standard response types and also utilizes inference to ensure our application code can effectively handle all potential scenarios: Visit the Playground However, ...

Angular 2 - Component Loading Screen

I'm looking for a solution for my loading component that shows a spinner and retrieves a remote configuration file necessary for the app to work. Is there a way to have all routes pass through the loading component first to ensure the config data is l ...

Are there any comparable features in Angular 8 to Angular 1's $filter('orderBy') function?

Just starting out with Angular and curious about the alternative for $filter('orderBy') that is used in an AngularJS controller. AngularJS example: $scope.itemsSorted = $filter('orderBy')($scope.newFilteredData, 'page_index&apos ...

Looking to access a component property from another component in Angular 14 – how can I do that?

I am facing a challenge where I need to access a string property from another component without displaying it in the html file. While searching for solutions, I come across references to *ngIf/ngFor, but those don't seem applicable to my scenario. My ...

Sending an HTTP POST request from an Angular 6 application utilizing Sendgrid and a NodeJs Google Cloud Function results in a Error 405 message

Attempting to access an Email through Sendgrid from an Angular 6 application with minimal payload. Utilizing a Google Cloud Function for the request leads to a 405 error from the browser: 405 Blocked by CORS policy: Response to preflight request doesn&ap ...

Experiencing difficulties with a click event function for displaying or hiding content

Struggling with implementing an onClick function for my two dynamically created components. Currently, when I click on any index in the first component, all content is displayed. What I want is to show only the corresponding index in the second component ...

Break down the key:value objects into individual arrays

I'm currently working on an ionic project that involves handling an incoming array composed of key:value objects such as the one shown in the image below: https://i.stack.imgur.com/qrZiM.png My question is, can these values be separated into three d ...

Is it possible to define a TypeScript class inside a function and access its parameters?

For example, in the world of AngularJS, you might see a construction like this: myApp.factory('MyFactory', function(injectable) { return function(param) { this.saySomething = function() { alert("Param=" + param + " inject ...