I'm trying to retrieve data from Angularfire : "^5.0.0-rc0" using a service, and want to return the result to the component using this service, and the component should print the result from firebase to html file.
I am facing issue due to async retrieval of data from firebase, although I am getting proper response in console but don't know how to get the data in my component's html file.
Please note : FirebaseObjectObservable<any[]>;
won't work in current version of angularfire, following are my files :
trip-data.service.ts :
import { Injectable } from '@angular/core';
declare var firebase: any;
import { Http, Response } from '@angular/http';
import { AngularFireDatabase, AngularFireObject } from 'angularfire2/database';
import { Observable} from 'rxjs/Observable';
@Injectable()
export class TripDataService {
finalItems;
public data: Array<any> =[];
public myData: any[] =[];
constructor(db: AngularFireDatabase) {
db.object('/').snapshotChanges().map(action => {
this.finalItems = { ...action.payload.val() };
return this.finalItems;
}).subscribe(item => {
console.log(item);
this.myData = item;
console.log(this.myData);
}
);
}
}
list.component.ts :
import { Component, OnInit } from '@angular/core';
import { TripDataService } from '../trip-data.service';
import { AngularFireDatabase, AngularFireObject } from 'angularfire2/database';
import { Observable} from 'rxjs/Observable';
@Component({
selector: 'app-list',
templateUrl: './list.component.html',
styleUrls: ['./list.component.css'],
providers: [TripDataService]
})
export class ListComponent implements OnInit {
trips;
constructor(service: TripDataService) {
// this.trips = service();
this.trips = service.myData;
console.log(this.trips);
}
ngOnInit() {
}
}