What is preventing me from retrieving a value from a member function or method within a TypeScript class instance?

I am facing an issue with the FileInfo class that implements the IFileInfo interface. This class has an instance member function ext and a function getExt(). Within my component, there is a private method named openTempFolder() which makes an HTTP call to retrieve an Array of FileInfo. However, I keep receiving a TypeError indicating that getExt() is not a function, and calling ext returns Undefined. What could be the mistake in my implementation?

Here is the relevant code snippet,

export class FileInfo implements IFileInfo {
  constructor(
    public exists: boolean,
    public length: number,
    public physicalPath: string,
    public name: string,
    public lastModified: Date,
    public isDirectory: boolean,
    public ext: () => string,
  ) {
    this.exists = exists;
    this.length = length;
    this.physicalPath = physicalPath;
    this.name = name;
    this.lastModified = lastModified;
    this.isDirectory = isDirectory;
    this.ext = () => {
      return this.name.replace(/^.*\./, '');
    };
  }
  getExt() {
    return this.name.replace(/^.*\./, '');
  }
}

When using the class in my component, it looks like this,

export class FileManagerComponent implements OnInit, OnDestroy {
  @ViewChild('fileManager') public fileManager;
  public contents: Array<FileInfo> = new Array<FileInfo>();
  private unsubscribe: Subject<void> = new Subject();

....

 private openTempFolder() {
    this.httpService
      .getRequest<Array<FileInfo>>('FileManager/OpenTemporaryDirectory/Uploads')
      .subscribe((r: HttpResponse<Array<FileInfo>>) => {
        this.contents = r.body;
        this.contents.forEach(e => {
          console.log(e.getExt()); // TypeError
          console.log(e.ext()); // Undefined
        });
      });
  }
}

Answer №1

Even though you specify the parameterized type in the httpService.getRequest() method, HTTP calls do not return objects of the FileInfo class. Instead, they return an array of plain JavaScript objects that do not contain the getExt() method.

For solutions to this issue, refer to this question for some helpful suggestions.

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 is the correct way to nest multiple ng-if statements?

I'm currently grappling with a problem involving multiple nested ngIf directives applied to ng-template elements in Angular.js, and I've yet to find the ideal solution. While I am aware of workarounds, they are not as efficient as I would like th ...

The response of the Typescript Subscription function

I'm struggling with retrieving the subscribe array in NG2. Being new to typescript, I find it difficult to understand how to pass variables between functions and constructors. This is what my code currently looks like: export class RosterPage exten ...

Retrieve the dimensions of an image once rendering is complete, using Angular

I'm currently working on obtaining the rendered size of an image within a component. By utilizing the (load) event, I can capture the size of the image as it appears at that particular moment (pic1), as well as its "final" size after the page has fini ...

Ways to verify if a function has completed execution and proceed to invoke another function

I am seeking to verify if a user has chosen an item from the ngFor form and then redirect them to another page upon submitting the form with the updated value. HTML: <mat-select placeholder="Treatment" [(ngModel)]="model.TreatmentA" name="TreatmentA" ...

What is the best way to access a state variable that has a union data type?

Is there a way to access a field of a state variable with a union type in TypeScript? Here is an example of a zustand store I have defined: import { create } from 'zustand' type HeightUnit = 'cm' | 'ft\' in"&ap ...

Transferring all instructions to a subordinate element within the component

I have developed a set of custom directives specifically for <input> elements. Additionally, I have created a custom component called <app-decorated-input>. Within my application, there are numerous instances of both <app-decorated-input> ...

Having trouble getting the ng-bootstrap Rating component to function on Angular 6?

I'm attempting to utilize Angular 6, ng-bootstrap, and the Rating feature, but I'm encountering issues. Does anyone have a solution for properly implementing this rating functionality? NG-RATE Here is my code: facilitistatus.component.html &l ...

Utilizing a string variable as a property name for dynamic typing

I am looking to dynamically create a type with a property name based on specified parameters. Although I can successfully create the object, I am facing challenges when trying to create the actual type. This dynamic type creation is essential for compositi ...

Exploring the narrowing capabilities of TypeScript within while loops

When I write while loops, there are times when I know for sure that a certain value exists (connection in this case), but the control flow analysis is unable to narrow it down. Here's an illustration: removeVertex(vertex: string) { const c ...

How can I dynamically change and load a configuration file based on the URL parameter using Angular?

My query involves modifying the config file on pageload based on a URL parameter. I currently have implemented the following: config-loader.service.ts @Injectable() export class ConfigLoaderService { constructor(private injector: Injector, private ht ...

Exploring Firestore Snapshots - Keeping an Ear Out for Fresh Documents

Currently, I am in the process of developing a dynamic scrolling pagination feature using Firestore. My approach involves loading a limit of 10 real-time documents into an array that will be displayed on the screen. The plan is to implement the loadMore() ...

Adding a dynamic CSS stylesheet at runtime with the power of Angular and Ionic 2

Currently, I am working on creating a bilingual application using Ionic2. The two languages supported are English and Arabic. As part of this project, I have created separate CSS files for each language - english.css and arabic.css. In order to switch be ...

What is the process for accessing a local JSON file and retrieving repetitive data from it?

As a newcomer to Angular 5, I am facing an issue with loading a JSON file and repeating its data. Although I can successfully retrieve and display data from the JSON file, I am unable to repeat the data. The JSON file is located at src/assets/data.json ...

What is the best method for incorporating a delay within the upcoming subscribe block in Angular?

When subscribing to a service method, I have a sequence of actions that need to occur: displaying a toaster, resetting a form, and navigating to another component. However, I want to introduce a delay before the navigation so users can see the toaster mess ...

What strategies can we use to minimize the number of times a service needs to be called when instantiating a recursive component that relies on it?

Check out this example of a component featuring the use of FormService in its providers: @Component({ selector: "app-block", templateUrl: "./block.component.html", styles: ['.block_children { padding-left: 50px;}'], providers: [FormServi ...

Tips on exporting a basic TypeScript class in an Angular 4 module

I am facing a challenge with packaging a TypeScript class as part of an Angular module for exporting it as a library using ng-packagr. For instance, here is my class definition - export class Params { language: string ; country: string ; var ...

Tips for providing a base URL in Angular to fetch images from an API

How do I access the API URL to retrieve images from a database? <table class="table"> <tr> <th>Id</th> <th>Image</th> </tr> ...

Handling errors in nested asynchronous functions in an express.js environment

I am currently developing a microservice that sends messages to users for phone number verification. My focus is on the part of the microservice where sending a message with the correct verification code will trigger the addition of the user's phone n ...

Why is ASP.NET Core returning an empty object in the response?

Upon debugging the code in VS, I noticed that the cities list I am returning contains 3 objects with properties. However, when I call the endpoint, I receive a response of 3 empty list items. How can I resolve this issue? Model Class: public class City ...

How can I determine if an Angular library is compatible with the specific version of Angular that my application is utilizing?

My Angular 8 application is currently running on a version that's quite outdated compared to the latest release of Angular. When it comes to incorporating Angular libraries, how can I determine if they are compatible with Angular 8? Is there a strict ...