Class with an undefined function call

Currently, I am working with angular2 and TypeScript where I have defined a class.

export class Example{

 //.../

   const self: all = this;
   functionToCall(){
      //.. Do somerthing
   }

   mainFunctionCall(){
      somepromise.then(x => self.functionToCall('url/'+ x.name ) )
   }
}

However, I keep encountering an error stating that functionToCall is undefined.

As I am fairly new to TypeScript/Angular, I wonder if there are any specific rules causing this issue. What is the correct way to call a method within a class from another method?

Answer №1

No need to resort to a workaround in this situation, as lambda functions automatically capture the this reference from the surrounding scope. This can simplify your code like so:

const somepromise = Promise.resolve({name:"noone"});

class Example {

    functionToCall(x : string) {
        console.log(x);
    }

    mainFunctionCall() {
        somepromise.then(x => this.functionToCall('url/'+ x.name ) )
    }
}

(new Example()).mainFunctionCall();

Edit Updated code snippet includes all necessary details and is runnable in the typescript playground.

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

What purpose does sending null to XMLHttpRequest.send serve?

Have you ever wondered why send is often called like this? xhr.send(null) instead of just xhr.send() ? W3, MDN, and MSDN all mention that the argument is optional. Additionally, the ActiveX control seems to work without it: hr=pIXMLHTTPRequest.Create ...

What is the most effective way to retrieve IDs from Firestore and add them to the object arrays?

Currently working on a project that involves React and Firestore, I'm attempting to retrieve the Ids back into the objects array. Below is the code snippet: function grabIds() { setIsLoading(true); reference.onSnapshot((querySnapshot) => ...

How to make an HTTP request in Angular 7 before the browser is closed

I'm attempting to trigger a POST HTTP request right before the browser closes. Essentially, I want to detect when the user closes the page and send a call, but the request isn't completing before shutdown. It seems like a traditional HTTP reque ...

Using arrays to pass parameters in JavaScript

I have multiple sliders that require the same set of parameters to be passed to each one. My understanding is that I need to use an array for this purpose. However, as a JavaScript novice, I am unsure of the correct way to implement it. The parameters lo ...

Come hang out in the user voice channel by reacting with your favorite emojis!

I am currently developing a Discord bot, and I want to implement a feature where the bot joins a voice channel if a user reacts to its message. I am using the awaitReactions function which only returns reaction and user data. Is there a way to retrieve th ...

Ways to store user data in AngularJS

Can anyone provide insights on how to securely save user information? I've heard suggestions like using $cookieStore, or Authentication, among others. What about utilizing $rootScope for this purpose? My thought is to store the user's ID and p ...

Reveal the class to the global scope in TypeScript

ClassExample.ts: export class ClassExample{ constructor(){} } index.html: <script src="ClassExample.js"></<script> // compiled to es5 <script> var classExample = new ClassExample(); //ClassExample is not defined < ...

How to locate the duplicate elements within an array using JavaScript

There are two different sets of numbers Numbers1=[ 0, 1, 2, 0, 2 ]; Numbers2=[ 0, 0, 1, 2, 2 ]; The goal is to determine the index of each element in Numbers2 from Numbers1 and create a new array like [0,3,1,2,4]; If you would like to see the code I wr ...

Sending a request from AngularJS to a Django API to retrieve a JWT token results in a 405 error response

After making a POST request to obtain the token in an endpoint that is supposed to accept POST requests, I keep receiving a 405 error. I am working with rest_framework and rest_framework_jwt Here is the code for the endpoint: from django.conf.urls impor ...

Utilize Angular 2 Form Elements Again

I'm currently in the process of working on a project with Angular and I want to make sure that my form components can be used for creating and updating entities seamlessly. Let's say I have a User entity stored on a remote API, and I have a form ...

``There seems to be a malfunction with the modal feature in the asp.net

Within my strongly typed view, I have a loop that iterates over a list of objects fetched from a database. Each object is displayed within a jumbotron element, accompanied by a button labeled "Had role before". When this button is clicked, a modal opens wh ...

The maximum nested function level has been reached at tsc:1. Consider increasing the FUNCNEST limit

Having an issue while trying to compile a typescript file, encountering the following error: work/gigMax [typescriptMigration●] » tsc src/mutate.ts tsc:1: maximum nested function level reached; increase FUNCNEST? work/gigMax [typescriptMigration●] ...

I'm having trouble with getting my Bootstrap CSS and JS links to function properly

The paths for the Bootstrap CSS and JS files have been included in the code, but unfortunately, they are not working. Instead, I am getting the following errors in the console: GET http://127.0.0.1:5000/[https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist ...

AngularJS Bindings Problem

As a newcomer to AngularJS, I have been working on creating a todo list. Through my research on Google, I was able to write the code successfully. Whenever I click the add button, whatever I type into the textbox gets added to the list (UL /li). However, ...

Methods for assigning values to a formControl using an array

I have an array of objects and I am attempting to loop through the array, dynamically setting values to a formControl and not displaying anything if the value is null. I have searched for similar solutions but haven't found any references or examples ...

Tips for incorporating a CSS transition when closing a details tag:

After reviewing these two questions: How To Add CSS3 Transition With HTML5 details/summary tag reveal? How to make <'details'> drop down on mouse hover Here's a new question for you! Issue I am trying to create an animation when t ...

The v-on:click event handler is not functioning as expected in Vue.js

I am currently learning vue.js and facing some challenges. Below is the code snippet from my HTML page: <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.jsdelivr.net ...

Having trouble resolving errors encountered while running the `npm run build` command, not sure of the steps to rectify

I am currently working on my first app and attempting to deploy it for the first time. However, I have encountered an error that I am unsure of how to resolve. When running "npm run build", I receive the following: PS C:\Users\julyj\Desktop& ...

In AngularJS, the process of replacing text using a filter can be achieved by following

Currently in the process of learning Angular, but I've hit a roadblock and can't seem to find a solution. I have a website that provides USPS shipping options, and I need to replace the default text with my own. The vendor's option shows &ap ...

Having difficulty selecting an item from the MaterialUI package

While trying to utilize the MaterialUI Select component with typescript/reactjs, I encountered an issue during the instantiation of the Select element. The error message I'm receiving states: Type 'Courses' is missing the following properti ...