Search code examples
angulartypescriptfirebasefirebase-realtime-databaseangular2-services

Data is not retrieving from Firebase database


This is my component. I don't know why data is not retrieving from firebase. Can anyone tell me the wrong in this code?

import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html'
})
export class DashboardComponent implements OnInit {
  tasks: FirebaseListObservable<any[]>;

  constructor(
    public afdb: AngularFireDatabase
  ) { 
      this.tasks = this.afdb.list('scheduler/')
      console.log("Console", this.tasks);      
  }

  ngOnInit() {
   }

}

When I execute this code, I'm getting this console -

 {query: Reference, update: ƒ, set: ƒ, push: ƒ, remove: ƒ, …}
 auditTrail
 :
 ƒ (events){query: Reference, update: ƒ, set: ƒ, push: ƒ, remove: ƒ,      …}
 auditTrail
 :
 ƒ (events)

Solution

  • The problem you have with this code is that you are trying to output an observable to the console when what you probably want to do is log the data the observable is watching.

    So you probably want to change:

        console.log("Console", this.tasks);
    

    to this:

        this.tasks.subscribe(items => {
            console.log(items);  
        });