0

When I try to inject a Service into my Component Angular won't load the component and throws an error instead. The error in the console just says:

ERROR Error: "[object Object]"
    Angular 11
        resolvePromise
        resolvePromise
        scheduleResolveOrReject
        invokeTask
        onInvokeTask
        invokeTask
        runTask
        drainMicroTaskQueue
        invokeTask
        invokeTask
        globalZoneAwareCallback

The component I'm trying to inject the service into (it is the workout-service):

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

import { CalendarService } from '../shared/calendar.service';
import { WorkoutService } from '../shared/workout.service';
import { Set } from '../shared/set.model';
import { Exercise } from '../shared/exercise.model';
import { Workout } from '../shared/workout.model';

@Component({
  selector: 'app-workout-form',
  templateUrl: './workout-form.component.html',
  styleUrls: ['./workout-form.component.css']
})
export class WorkoutFormComponent implements OnInit {
  exerciseArr: number[] = [];

  selectedDay = '1';
  selectedMonth = 'Mai';
  selectedYear = '2019';

  constructor(public calendarService: CalendarService, private workoutService: WorkoutService) { }

  ngOnInit() {
  }
[...]

And here is the service that I'm trying to inject:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { AuthService } from '../auth/auth.service';

@Injectable({
  providedIn: 'root'
})
export class WorkoutService {

  constructor(private http: HttpClient, private authService: AuthService) { }

  saveWorkoutToDb(workout) {
    this.http.post([...]);
  }
}

My IDE gave me the error message that it can't resolve all parameters for the given component. When I remove the injection, the component gets loaded fine.

This is my app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatCardModule,  MatInputModule, MatButtonModule } from '@angular/material';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';


import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { WorkoutFormComponent } from './workout-form/workout-form.component';
import { WorkoutCalendarComponent } from './workout-calendar/workout-calendar.component';
import { WorkoutEditComponent } from './workout-edit/workout-edit.component';
import { AuthComponent } from './auth/auth.component';

@NgModule({
  declarations: [
    AppComponent,
    WorkoutFormComponent,
    WorkoutCalendarComponent,
    WorkoutEditComponent,
    AuthComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    BrowserAnimationsModule,
    MatCardModule,
    MatFormFieldModule,
    MatInputModule,
    MatButtonModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Edit: it seems that when I remove the injection of the HttpClient in the workout service, the error disappears.

6
  • Can you inject the service elsewhere, e.g. does it work in your tests? Commented May 23, 2019 at 20:05
  • Can you show us your app.module.ts (the module of the component) ? Commented May 23, 2019 at 20:06
  • I tried to inject the service in another component and it threw the same error. Commented May 23, 2019 at 20:08
  • 1
    did you miss putting your service in providers array? Commented May 23, 2019 at 20:12
  • 1
    Try to add "HttpClientModule" into imports array in your app.module.ts Also use this import: import { HttpClientModule } from '@angular/common/http'; Commented May 23, 2019 at 20:23

2 Answers 2

1

You need to change your app.module.ts like this:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatCardModule,  MatInputModule, MatButtonModule } from '@angular/material';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';


import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { WorkoutFormComponent } from './workout-form/workout-form.component';
import { WorkoutCalendarComponent } from './workout-calendar/workout-calendar.component';
import { WorkoutEditComponent } from './workout-edit/workout-edit.component';
import { AuthComponent } from './auth/auth.component';

@NgModule({
  declarations: [
    AppComponent,
    WorkoutFormComponent,
    WorkoutCalendarComponent,
    WorkoutEditComponent,
    AuthComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
    BrowserAnimationsModule,
    MatCardModule,
    MatFormFieldModule,
    MatInputModule,
    MatButtonModule,
    HttpClientModule, //<- you are missing this import 
  ],
  providers: [],
  bootstrap: [AppComponent]
})
Sign up to request clarification or add additional context in comments.

6 Comments

Did you add all the services? You are also using AuthService and CalendarService
Yes, even if I add all the services I'm using to the providers array I still get the same error that only appears when I try to load a component in which I inject this workout service.
Did you miss the fact that the service is declared with providedIn: 'root' in the @Injectable decorator? That makes adding the service to the module unnecessary. @AsimHashmi
I just found out that injecting the service works when I remove the injection of the HttpClient into the workout service. Does anyone have an idea what causes this behaviour?
@fonzane I think your missing the import for HttpClientModule in your app.module.ts. That's why it throws error when you inject HttpClient
|
0

The same happened to me, you need to add 'HttpClientModule' class to the imports in the app.module.ts file. Just remember it need to be in the root module, declaring this class in child modules will not work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.