Search code examples
angulartypescriptfirebasefirebase-realtime-databaseangularfire2

Undefined variable while it is declared


I m creating a small app that reads data from Firebase and store it in table named "actualiteNews" it tells me that it is not defined while it is declared

import { Injectable} from '@angular/core';
import {AngularFireDatabase} from 'angularfire2/database';

@Injectable({
  providedIn: 'root'
})
export class ActualiteService  {
  actualiteNews : any[];


  constructor(dataBaseFire : AngularFireDatabase) {
    dataBaseFire.list('/news').valueChanges().subscribe(actualiteNews => {
      this.actualiteNews = actualiteNews;
      console.log(this.actualiteNews);
    });

   }

getActualite(Temptitre:string){
    return this.actualiteNews.find(value =>value.titre === Temptitre);
  }

  getActualiteNews(){
    return this.actualiteNews;
  }
}

Solution

  •  actualiteNews : any[] = [];
    

    Yes it is declared but you have to set the default value as empty array. You might be doing actualiteNews.length in template but you haven't defined actualiteNews. That is the reason it is breaking.