Mr. Toast
Mr. Toast

Reputation: 1869

Angular 5, NullInjectorError: No provider for Service

Hello im trying to implement firestore into my web application, when i add the service to contructor the error:

NullInjectorError: No provider for TesteventService!

I'm using Angular 5, angularfire2/firestore and typescript 2.7.1

testevent.service.ts

import { Injectable } from '@angular/core';
import { AngularFirestore } from 'angularfire2/firestore'

@Injectable()
export class TesteventService {

  constructor(private afs: AngularFirestore) { }

  addEvent(eventData){
    this.afs.collection('testevent').add(eventData).then(() => {
      console.log('Done');
    })
  }

  getEvent(){
    return this.afs.collection('testevent', ref => ref.orderBy('id')).valueChanges()
  }
}

component.ts

import { Component, OnInit } from '@angular/core';
import { TesteventService } from '../../providers/testevent.service';
import { AngularFirestore } from 'angularfire2/firestore';

export class CsvImportComponent implements OnInit {
  data_rows = []

  constructor(private event: TesteventService){})

When I add events from TesteventSerivice i get the error, while nothing is run.

Upvotes: 58

Views: 143753

Answers (3)

Shoaib Khalil
Shoaib Khalil

Reputation: 2430

There are two possibilities to solve this problem:

  1. You can provide your service name in app.module.ts providers array like this way and error will be gone:
imports: [
],

declarations: [
],

providers: [  TesteventService ]
  1. For some of the latest versions of angular you can simply annotate you desired service this way:
@Injectable({ providedIn: 'root' })

Upvotes: 16

TARJU
TARJU

Reputation: 1994

Here is how i have solved my problem with how to pass runtime parameter to a angular service which is part of another custom library

It was difficult to identify where and how to provide injected parameter

Here my AuthService is part of another library i used providedIn: 'root' because my service requirement was to define it as a singleton service. And there are many ways to achieve that ...

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

 constructor(
        @Inject('defaultUserName') private defaultUserName: string,
        @Inject('defaultPassword') private defaultPassword: string
    ) {
      // Now you can use injected parameters
        console.log("defaultUserName", defaultUserName);
        console.log("defaultPassword", defaultPassword);
      
    }

  // More code and methods

}

Here is how i use above service in my project

import { CoreApiModule } from 'my-library/app';
import { **AuthService** } from 'my-library/app/api/auth.service';

@NgModule({
    imports: [
        CoreApiModule
    ],

    declarations: [
    ],

    bootstrap: [AppComponent],
    providers: [
        AuthService,
        {
            provide: 'defaultUserName',
            useValue: Constants.userName,
        },
        {
            provide: 'defaultPassword',
            useValue: Constants.defaultPassword,
        }
    ]
})
export class AppModule {}

Now you can directly import AuthService in any component and start using it.

Upvotes: 3

Sajeetharan
Sajeetharan

Reputation: 222722

You need to add TesteventService under providers under imports in your app.module.ts

providers: [
  TesteventService 
]

Upvotes: 109

Related Questions