0

I have the following class FileInfo that implements IFileInfo interface. The class defines an instance member function ext and a function getExt(). in my component I have a private method called openTempFolder() which is basically an http call that return an Array of FileInfo. I keep getting a TypeError that getExt() is not a function and ext keeps returning Undefined upon calling them. What am I doing wrong here?

I am posting the relevant code,

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(/^.*\./, '');
  }
}

in my component I call this way,

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
        });
      });
  }
}
JSON
  • 1,583
  • 4
  • 31
  • 63

1 Answers1

1

The response of HTTP calling doesn't generate objects of FileInfo class in spite of you define the parametrized type of httpService.getRequest() method. The response is at really an array which elements are plain JavaScript objects that obviouslly doesn't have the getExt() method.

See this question. It contains some suggestions to fix it.

Rodrigo
  • 2,313
  • 2
  • 15
  • 23