Angular 5, NullInjectorError: No provider for Service

Mr. Toast picture Mr. Toast · Mar 5, 2018 · Viewed 34.9k times · Source

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.

Answer

Sajeetharan picture Sajeetharan · Mar 5, 2018

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

providers: [
  TesteventService 
]