Error: The function _this. is not callable

I encountered a perplexing issue while attempting to call a service function within the subscribe() method of another service function call. The error message indicated a TypeError:

TypeError: _this.fileloaderService.downloadFile is not a function

I have two services in place, one for fetching episodes and the other for loading file contents. Both are imported and included in the EpisodesComponent constructor as shown below:

import { EpisodeService } from '../episode.service';
import { FileloaderService } from '../fileloader.service';

constructor(private episodeService: EpisodeService,
  private fileloaderService: FileloaderService) { }

After retrieving the episodes, I attempted to load a file from a URL mentioned in each episode object:

getEpisodes(): void {
  this.episodeService.getEpisodes().subscribe(episodes => {
    this.episodes = episodes;
    for (let i of this.episodes) {
      this.fileloaderService.downloadFile(i.fileUrl).subscribe(file => {
        i['file'] = file;
      });
    }
  });
}

Despite having defined the downloadFile() method in the service, an error was raised in the console suggesting that the method does not exist or is not instantiated. Both services were configured identically.

Any insights would be greatly appreciated! (I'm still learning Typescript so please bear with me!) Thank you...

EDIT 1: Running ng serve --open, I added:

console.log("fileloaderService:" + JSON.stringify(this.fileloaderService));

within the getEpisodes().subscribe() function, which resulted in the following output:

fileloaderService: {}

EDIT 2: I also included the service in app.module.ts providers like so:

providers: [EpisodeService, FileloaderService],

EDIT 3: A simplified version of the app can be viewed on StackBlitz here:

https://stackblitz.com/edit/angular-sazw43

Currently facing issues with the in-memory-web-api setup but all relevant details should be accessible (I hope).

Answer №1

Hey, could you give this a shot?

fetchEpisodes(): void {
  this.episodeService.fetchEpisodes().subscribe(episodes => {
    this.episodes = episodes;
    var self = this;
    for (let episode of this.episodes) {
      self.fileLoaderService.loadFile(episode.fileUrl).subscribe(file => {
        episode['file'] = file;
      });
    }
  });
}

Answer №2

It appears that the issue stemmed from the incorrect formatting of the http.get() function within the downloadFile() method, as it did not accept a RequestOptions object as intended.

I suspect this error occurred due to utilizing the outdated @angular/http library instead of the more current @angular/common/http

Apologies for any inconvenience caused by this oversight.

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

Using Angular and Typescript to implement mathematical formulas involving date object subtraction

I need help converting the following Excel formula to Typescript. I keep running into an error that says 'The left-hand and right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type'. Can anyon ...

An issue has occurred: Failure to execute spawnSync PATH/target/node/node ENOENTNGCC. Please refer to the

Attempting to initiate my angular project using ./mvnw is resulting in an error when the build runs ng build --configuration development. The error message thrown reads as follows: Generating browser application bundles (phase: setup)... [INFO] /home/use ...

Initializing a component in Angular 2 using third party callbacks

My ultimate objective is to create a custom Google Maps component using Angular2. The Google Maps API comes with its own library that can be initialized using a <script> tag either with or without a callback function. An example of initializing it ...

What is the best way to redirect to a different URL in angular after signing in with AWS Amplify?

Currently, I am utilizing the latest authentication component from AWS-Amplify. While I can successfully log in, I am facing an issue where the URL remains the same after logging in, instead of redirecting to a custom URL as desired. To provide some contex ...

Issue experienced with Nebular's NbRoleProvider when running in a live environment

Here is my role.provider.ts: import { Injectable } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import { map } from 'rxjs/operators/map'; import { NbAuthService, NbAuthJWTToken } from '@nebular/aut ...

Can you use getters and setters in a TypeScript declaration file?

I am facing an issue with a declaration file for the openUi framework. The framework utilizes a get<propname>() and set<propname>(var) syntax for its properties. In traditional JavaScript, the setup would look like this: sap.ui.getCore().atta ...

Is it time to refresh the package-lock.json file?

I'm currently in the process of updating a React app from Node 14 to 16. One step I took during the upgrade was deleting the node_modules folder and package lock, then generating a new package-lock.json file. However, this resulted in numerous compila ...

Integration of NextAuth with Typescript in nextjs is a powerful tool for authentication

I am diving into NextAuth for the first time, especially with all the new changes in Nextjs 13. Setting up nextauth on my project seems to be a daunting task. I have gone through the documentation here I am struggling to configure it for nextjs 13. How do ...

The selected attribute does not function properly with the <option> tag in Angular

Currently, I am faced with a challenge involving dropdowns and select2. My task is to edit a product, which includes selecting a category that corresponds to the product. However, when I open the modal, the selected group/category is not displayed in the d ...

Dynamically loading classes in TypeScript without using default export

Is there a way to dynamically load classes in TypeScript without using a default export method? I have managed to make it work partly, but I am looking for a solution that doesn't require a default export: export default class Test extends Base { ... ...

Sticky-top Navbar in Angular5 and Bootstrap4

The "sticky-top" Bootstrap navbar position only functions if the navbar is a direct child of: <body> <header class="sticky-top"> <nav class="navbar navbar-light bg-light p-0"> ... </nav> </header> </bod ...

Module 'bcryptjs' could not be located

Recently, I added the @types/bcryptjs package to my Node.js project. Initially, there were no issues with importing it. However, when I attempted to use it in my code by including the line: console.log(bcrypt.hashSync(req.body.password)) I encountered an ...

What could be causing TypeORM to create an additional column in the query

Why does this TypeORM query produce the following result? const result6 = await getConnection() .createQueryBuilder() .select('actor.name') .from(Actor,'actor') .innerJoin('actor.castings',&apos ...

How to efficiently upload multiple files simultaneously in Angular 10 and .NET Core 5 by utilizing a JSON object

I have a JSON object structured like this: Class->Students this is a basic representation of my TypeScript class export class Classroom { Id:number; Name:string; Students:Student[]=[]; } export class Student { Name:string; Age:number; Sex:string; Imag ...

Tips for sending a variable to control a particular panel within an accordion in Angular 2

I have a collection of ngbpanels that are dynamically created using ngFor. I need to expand or collapse a specific panel based on certain conditions, but the ID I provide for the panel is stored in a variable. The code does not recognize the panel ID when ...

What is the best way to implement multiple ternary operators within HTML code?

Consider the following code snippet: It currently applies CSS classes up to red4, but I want to apply CSS classes up to red11. Additionally, the variable "size" in myData should be dynamic. For example, size could range from 0-20 // 0-100 // 0-10000, etc., ...

How can I effectively integrate TypeScript with Jest to mock ES6 class static properties?

For the purpose of simulating payment failures in my Jest tests, I have developed a mock file for mangopay2-nodejs-sdk: // __mocks__/mangopay2-nodejs-sdk.ts import BaseMangoPay from 'mangopay2-nodejs-sdk'; export default class MangoPay extends B ...

Software designed for apple silicon ARM64 running Electron framework

Running the Electron desktop application successfully on X64 using electron-builder and dmg. Now, if we need to run the same application on Apple Silicon (ARM64), we followed the steps below: Installed Xcode 12 and upgraded Mac to Big Sur. Executed " ...

Is it possible to enhance the configurations within angular.json?

When creating my Angular 6 application, I encounter the need to define two key aspects simultaneously: Whether it's a production or development build The specific locale being utilized Within my angular.json, I have the following setup: "build": { ...

Encountering an issue while trying to select a checkbox that contains multiple images

In my Angular 7 application, I am working with a scenario where I have to select one image from various other images. To achieve this, I have utilized checkboxes for each image, but encountered a problem. Since the images are fetched from a service and i ...