Error: Unhandled rejection - HTTPService provider not found

I'm in the process of migrating my app to the latest RC version and I've encountered some errors that I'm unable to resolve. Despite conducting an extensive search before seeking help here, I haven't been able to find a solution.

Upon clicking a button, the following should load:

Below is the component code:

import { Component, OnInit } from '@angular/core';

import { HTTPService } from '../shared/api.service';
import { Server } from './server.model';
import { SharedService } from '../shared/moveData.service';


@Component({
    templateUrl: './mainserverpage.template.html',
})
export class MainServerPage implements OnInit {

    constructor(private _httpService: HTTPService, 
                private _moveData: SharedService) { }

    errorMessage: string;
    public servers: Server[];
    isLoading = true;
    tillLoading;
    selectedServer: Server;
    currentServer;
    isServerOnline=false;

    ngOnInit() {
          this.getServers('qa');
    }

    reloadServers(env) {
        this.servers = null;

        this.getServers(env);
    }

    getServers(env?) {
        this._httpService.getServers(env)
            .subscribe(
            value => {
                this.servers = value;
                this.isLoading = false;
            },
            error => this.errorMessage = <any>error);
    }
}

The module code is quite straightforward:

import { NgModule }            from '@angular/core';
import { CommonModule }        from '@angular/common';
import { HttpModule }          from '@angular/http';

import { SharedModule }        from '../shared/shared.module';

import { MainServerPage }      from './mainserverpage.component';
import { Server }         from './server.model';
import { HTTPService } from '../shared/api.service';

@NgModule({
    imports: [
        CommonModule,
        SharedModule,
        HttpModule,
    ],
    declarations: [
        MainServerPage
    ],
    exports: [
        MainServerPage
    ],
    providers: [
        HTTPService
    ]
})
export class MainserverpageModule {
}

The routing setup:

import { RouterModule  }     from '@angular/router';

import { MainServerPage }    from './mainserverpage.component';


export const mainRouting = RouterModule.forChild([
    { path: 'mainserverpage', component: MainServerPage }
]);

Lastly, the app.module that I'm utilizing. Omitting the template for brevity:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { SharedModule } from '../app/shared/shared.module';
import { MainserverpageModule } from '../app/servers/mainserverpage.module';

import { HomeComponent }     from './home.component';
import { NavBarComponent }   from './navbar.component';
import { NotFoundComponent } from '../app/shared/notfound.component';
import { MainServerPage } from '../app/servers/mainserverpage.component';

import { routing }           from './app.routing';
import { mainRouting } from '../app/servers/mainserverpage.routing';

@NgModule({
  declarations: [
    AppComponent,
    NavBarComponent,
    HomeComponent,
    NotFoundComponent,
    MainServerPage
  ],
  imports: [
    BrowserModule,
    FormsModule,
      HttpModule,
    SharedModule,
    routing,
    mainRouting
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

The error I'm facing when trying to load this is as follows:

zone.js:420 Unhandled Promise rejection: No provider for HTTPService! ; Zone: angular ; Task: Promise.then ; Value: NoProviderError {__zone_symbol__error: Error: DI Error at NoProviderError.ZoneAwareError (http://localhost:4200/vendor.bundle.js:87143:…, _nativeError: ZoneAwareError, keys: Array[1], injectors: Array[1], __zone_symbol__message: "No provider for HTTPService!"…}__zone_symbol__error: Error: DI Error at NoProviderError.ZoneAwareError

While the solution might be simple for someone else, I'm unable to pinpoint the issue at my end. Thank you for your assistance.

Answer №1

Make sure to include your MainserverpageModule in the imports section of your app.module. Right now, you have only added the MainServerPage component and not the associated service.

@NgModule({
  declarations: [
    AppComponent,
    NavBarComponent,
    HomeComponent,
    NotFoundComponent
    //Exclude this component
  ],
  imports: [
    BrowserModule,
    MainserverpageModule <-- Remember to add this module
    FormsModule,
    HttpModule,
    SharedModule,
    routing,
    mainRouting
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Answer №2

To ensure your component is aware of the providers, you must include the following code snippet:

import { SharedService } from '../shared/moveData.service';

@Component({
    templateUrl: './mainserverpage.template.html',
    providers: [SharedService]   <= remember to inject your services here
})
export class MainServerPage implements OnInit {

    constructor(private _httpService: HTTPService, 
                private _moveData: SharedService) { }

}

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 best way to utilize Typescript when making queries to Firebase?

I have successfully integrated push notifications for my app using Firebase Cloud Functions. Now, I am looking to enhance the user experience by updating the app's badge count alongside the push notifications. I understand that this can only be achiev ...

Angular ngx-translate Feature Module failing to inherit translations from Parent Module

Currently, I am implementing lazy loading for a feature module. I have imported TranslateModule.forChild() with extend true to load feature-specific translations. In my app.module, I am importing TranslateModule.forRoot to load common translations. The i ...

Dynamically set the value of a form in <mat-select> using binding

In my 'div' element, I have implemented a "mat-select" feature that contains a dropdown of country lists in a multi-select format. I am looking to automate the process of populating the countries within the "mat-select" when a button is clicked. ...

Encountering an issue when trying to upload a photo from Angular 8 to Laravel: receiving a "Call to a member function extension() on null" error

In my project using Angular 8 for the front end and Laravel 5.8 for the backend, I encountered an issue with uploading photos. I found guidance in this tutorial from ACADE MIND. Here is my template code : <input *ngIf="photoEdit" enctype="multipart/ ...

Customizing primary button colors in Bootstrap 5.2

I've been attempting to customize the default colors in Bootstrap 5.2.3 within my Angular 15 application, specifically to make .btn-primary reflect my app's primary color, but so far I haven't been successful. Here's a snippet from my ...

Issue with page reload in Angular 8 when URL contains a dot

When the URL contains a dot(.) and we attempt to reload the page using the browser's reload function, the following error occurs: The resource you are seeking has been deleted, its name has been changed, or it is currently unavailable. We prefer not ...

Using TypeScript, add an observable to an array of observables using RxJS

Can someone explain how to include Observable<News> in an array of observables? newsArray: Observable<Array<any>>; addNews(newsId: number) { let news = this.newsService.getNewNews(newsId); // this results in an Observable<News> ...

Guide on integrating an Excel file into the ag-grid-angular platform

I was looking for the import method, but I couldn't seem to find it. Does anyone have any idea where it is located? Please inform me! ...

When adding new elements to an array, the IDs of all objects become identical

When running the code below: dietDay.meals.forEach((meal) => { meal.mealProducts.forEach((mealProduct) => { if ( mealProduct.product.id && this.fetchedProductIds.includes(mealProduct.p ...

What is the process for importing a TypeScript module in the StackBlitz editor?

When I enter the editor at Stackblitz.com and start a new Angular project, it comes with default files and folders already set up. In the "Dependencies" section, I decide to add shortid. So, I input that in the designated box and it begins loading the cor ...

Sending information to a deeply nested child component in Angular 4

Here is the structure of components in my Angular application: app.component.html units.component.html section.component.html {{appData.title}} I have created "appData" in the app.component.ts and now I want to access it in the third level child co ...

Unable to direct XHR requests even though the URL is functional in the browser

I am currently working on developing a REST API in TypeScript using NodeJS and Express. So far, the API is up and running on localhost. When I visit http://localhost:8080, everything functions as expected. Specifically, I have configured the route http:/ ...

Identify potential interference with Sentry.captureMessage(...) or Sentry.captureException(...) functions within a JavaScript application, such as being obstructed by an ad-blocking extension

Overview Our Javascript application uses Angular and TypeScript, with Sentry.io for error reporting. If an error occurs, a custom modal allows users to leave a message, which should be sent to Sentry when they click submit. Issue We have noticed that mes ...

What methods exist for creating visual representations of data from a table without relying on plotting libraries?

Is there a way to plot graphs directly from a Data Table without the need for external graph libraries like plotly or highcharts? Ideally, I am looking for a solution similar to ag-grid where the functionality comes built-in without requiring manual code ...

Using Angular 2 to turn router parameter into a string

I am trying to format the URL as follows: user/felicia.christensen Within my template, I am using the following code: <a [routerLink]="['/user', user.userName | lowercase]">{{user.name}}</a> When user.userName returns felicia.chr ...

Error Message: Incompatibility with Angular "npm i" Engine Detected

When trying to install all the dependencies for my Angular application, I encountered an error message after entering npm i. The error message can be found here. Initially, I thought the issue might be related to installing Angular CLI globally using npm. ...

Karma Unit test: Issue with accessing the 'length' property of an undefined value has been encountered

While running karma unit tests, I encountered a similar issue and here is what I found: One of my unit tests was writing data to a json file, resulting in the following error: ERROR in TypeError: Cannot read property 'length' of undefined a ...

Facing an issue with displaying a component error in a mat-form-field in Angular 9

I am looking to develop a shared component for displaying errors in Angular Material. Here is my shared component pfa-share-error: <mat-error *ngIf="fieldErrors(fieldName).required && fieldErrors(fieldName)"> Required </mat-err ...

A solution for resolving the RuntimeException in genuitec's TypeScript Editor for Eclipse Oxygen

After setting up a fresh Eclipse Oxygen and installing the Angular IDE from Genuitec, I encountered an issue on the second day when I opened a project and accessed a *.ts File. The error message displayed was: java.lang.RuntimeException: java.lang.Illegal ...

Converting Objects to Arrays with Angular Observables

After searching extensively on SO for answers regarding item conversions in Javascript/Angular, I couldn't find a solution that addresses my specific problem. When retrieving data from Firestore as an object with potential duplicates, I need to perfor ...