Angular 13 throwing error: @Injectable returning undefined

Hey everyone, I recently upgraded my Angular to version 13 and I'm facing some issues with DI. Whenever I call the

ReflectiveInjector.resolveAndCreate()
function, I get an undefined error. Any idea what could be causing this? I'm specifically having trouble with ScheduleProvider, which is decorated with @Injectable() and injected into my pro-container.js file.

Here are the relevant segments:

schedule-provider.js

import { Injectable } from 'injection-js';

import { MzpackgHttpService, MzpackgLightResource, MzpackgListFilter } from 'mzpackg-api-schedules';

@Injectable()
export class ScheduleProvider {
public schedule= {
/*
  some data
*/
};

public constructor(private readonly mzPackgHttpService: MzpackgHttpService) {}
}

pro-container.js

import { InjectionToken, Provider, ReflectiveInjector } from 'injection-js';

import {
 RolesPartiesHttpService,
 HttpService,
} from 'mzpackg-api-roles';

import {PaymentsHttpService } from 'mzpackg-api-payments';
import { LawHttpService } from 'mzpackg-api-law';
import {
TasksHttpService,
} from 'mzpackg-api-tasks';
import { BrokersHttpService } from 'mzpackg-api-brokers';
import {
 HttpService as ExtendedPaymentHttpService
} from 'mzpackg-api-extpayments';

import { ScheduleProvider } from './schedule-provider';

export const BROKERS_HTTP_SERVICE = new InjectionToken<HttpService>('broker http service');
export const PAYMENTS_HTTP_SERVICE = new InjectionToken<ExtendedPaymentHttpService>('payments http service');

export class ProContainer{
  private readonly providers: Provider[];

  public constructor() {
    this.providers = [
      {
        provide: BrokersHttpService,
        useFactory: (httpService: HttpService) => new BrokersHttpService(httpService),
        deps: [BROKERS_HTTP_SERVICE]
      },      
      {
        provide: PaymentsHttpService,
        useFactory: (httpService: HttpService) => new PaymentsHttpService(httpService),
        deps: [PAYMENTS_HTTP_SERVICE]
      },
       {
        provide: ScheduleProvider,
        useFactory: (tasksHttpService: TasksHttpService) => new 
ScheduleProvider(tasksHttpService),
        deps: [TasksHttpService, RolesPartiesHttpService, LawHttpService]
       },
    ];
  }

}

The issue seems to arise when I comment out ScheduleProvider; everything works fine then. It appears that ScheduleProvider is causing the undefined error. My Angular package versions are as follows:

@angular-devkit/architect       0.1302.6

@angular-devkit/build-angular   13.2.6

@angular-devkit/core            13.2.6

@angular-devkit/schematics      13.2.6

@angular/cdk                    13.2.6

@angular/cli                    13.2.6

@angular/compiler-cli           13.2.6

@angular/language-service       13.2.6

@angular/material               13.2.6

@schematics/angular             13.2.6

rxjs                            6.6.7

typescript                      4.5.5

Any assistance would be greatly appreciated.

Answer â„–1

When referring to the official documentation:

It is noted that starting from version 5, the usage of ReflectiveInjector is deprecated due to its slow performance and heavy code overhead. It is recommended to use Injector.create instead.

As per the instructions in the official documentation:

const injector: Injector =
    Injector.create({providers: [{provide: 'validToken', useValue: 'Value'}]});
expect(injector.get('validToken')).toEqual('Value');
expect(() => injector.get('invalidToken')).toThrowError();
expect(injector.get('invalidToken', 'notFound')).toEqual('notFound');

Furthermore, the Injector function will return itself when provided with Injector as a token:

const injector = Injector.create({providers: []});
expect(injector.get(Injector)).toBe(injector);

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 could be causing my component to fail to re-render in this task management application?

Regarding MobX, my understanding is that the observable (the task list) should trigger a rerender of the observer (the TaskList component) when it undergoes changes. The action of pushing task data into the observable state should modify the observable, ri ...

When the tabBar is hidden on a particular screen, a gray area is shown at the bottom of the page in the Expo app

I am currently working with nested routes using expo-router. When attempting to hide the tabBar for a specific page, I noticed that it still displays a gray area. To address this issue, I have set the display option to none in the tabBarStyles options and ...

Leveraging Axios interceptors in Typescript

How can I effectively utilize axios interceptors with TypeScript? import axios, { AxiosRequestConfig, AxiosInstance } from 'axios' HTTP.interceptors.request.use((config: AxiosRequestConfig) => config) For instance, when creating an axios in ...

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 ...

Type children are not permitted in the TypeScript container

Container is a component that extends from @material-ui/core and does not accept children of type React.ReactNode. Layout.tsx: import { Container } from "@material-ui/core"; type LayoutProps = { children: React.ReactNode; }; function Layout( ...

Creating a customized bar chart in Angular using d3 with specific string labels - a step-by-step guide

I am currently implementing a bar chart using d3 in Angular to represent feelings ranging from very bad (1) to very good (5), with the feelings as labels on the yAxis. However, I am encountering an error message: Argument of type '(d: any, i: any) =&g ...

What is the best way to incorporate the async pipe into my codebase when working with GraphQL queries?

I am currently working on an Angular project where I utilize GraphQL to fetch data in my component. In the HTML code, I display an image of the result. However, I encountered an error in the console that said: ERROR TypeError: Cannot read property 'im ...

Struggling with aligning mat-icon in the center using HTML and CSS?

My issue is that the mat-icon in my generated columns is not center aligned. What could be causing this? When using ngFor to generate my datatable columns dynamically, none of them align correctly. The mat-icon inside my DIV defaults to left alignment. ...

Receiving information from the Angular server located at IP address 127.0.0.1

Working on a project that utilizes Ionic, Angular, NodeJS, and Express. Seeking to fetch data from an application on my laptop located at: Currently using a proxy for local development, but unsure how to handle this after deploying to Heroku. Seeking gui ...

Uploading and parsing multiple JSON files using JavaScript and Vue.js

I have been working on uploading and saving multiple JSON files. In the past, I had successfully implemented code for uploading and saving a single JSON file using the following approach: <input type="file" id="file" ref="fileSe ...

Transform TypeScript interface to a JSON structure

Searching for a method to convert a typescript interface into a JSON object? I'm in need of a function that can take an interface as a parameter. function transformInterfaceToResponse(input: any): DesiredInterface { const response = { descripti ...

Creating services in Angular is a continuous process

How can I create a service as singleton in Angular? I have a service that is injected into 2 components and the value is set to true. However, every time I open the view, the service is created again and the value resets to false. How can I make sure the ...

Eliminating every instance of the character `^` from a given string

I am encountering an issue with a particular string: "^My Name Is Robert.^". I am looking to remove the occurrences of ^ from this string. I attempted using the replace method as follows: replyText.replace(/^/g, ''); Unfortunately, thi ...

What is the best way to trigger a function in the parent component when a child component is clicked

I'm facing a scenario where I have two components - a parent and a child. Within the child component, there is a button. My goal is to trigger a method in the parent component when the user clicks on that button within the child component. Any ideas o ...

How can we fetch data from the server in Vue 2.0 before the component is actually mounted?

Can anyone help me with this question noted in the title? How can I prevent a component from mounting in <router-view> until it receives data from the server, or how can I fetch the data before the component is mounted in <router-view>? Here a ...

The container struggles to contain the excess of images spilling over

I'm having trouble getting a group of images to stay within their container in a grid layout, as they're overflowing vertically beyond the container's boundaries. Is there a way to adjust their size so they match the height of their parent ...

In the application I'm developing, I'm utilizing React alongside TypeScript, making use of the useContext and useReducer hooks. However, I've encountered an issue where the dispatch function is not being

Currently, I have implemented a feature where two lists are displayed as cards based on one main list of objects. The goal is to toggle the 'favorite' value for each card item when the star button is clicked. This action should move the favorited ...

Add a Filter to the Observer (__ob__) in Typescript

I am trying to implement a filter using this.Grid.option("dataSource").filter(x => x.Placeholder != null) however, it doesn't seem to be working when I run console.log(this.Grid.option("dataSource")); I receive (72) [{…}, {…}, {…}, {†...

Modify the Text Displayed in Static Date and Time Picker Material-UI

Looking to update the title text on the StaticDateTimePicker component? Check out this image for guidance. In the DOM, you'll find it as shown in this image. Referring to the API documentation, I learned that I need to work with components: Toolbar ...

Fields may be designated as either optional or required depending on the type parameters that

I am attempting to specify that the payload field should be mandatory only when T is defined: export interface Action<T = any> { readonly type: string; readonly payload?: T; } // The payload field must be included const actionWithPayload: Act ...