I am trying to retrieve data from Firebase within a service and use it in a component in order to display the data on my template. I'm using Angularfire2 & Firebase.
Service code:
export class MaterialDataFirebaseService {
constructor(private db:AngularFireDatabase) {
}
getData(group, endKey?) {
let query = {
orderByKey: true,
limitToFirst: group
};
if(endKey) query['startAt'] = endKey;
// tried the two commented blocks below, but gave errors
// return this.db.list('/', {
// query
// });
// return this.db.list('/', ref => {
// let q = ref.limitTolast(25).orderByKey(true);
// return q;
// });
this.db.list('/', ref =>
ref.orderByKey().limitToFirst(group))
.valueChanges()
.subscribe(materialData => {
return materialData;
});
}
}
Component code:
export class LibraryMaterialCardComponent implements OnInit {
materials: Material[];
firebaseData = new BehaviorSubject([]);
group = 8;
endKey = '';
finished = false;
constructor(private dataService:DataService, private _materialDataFirebaseService: MaterialDataFirebaseService) {
}
ngOnInit() {
this.dataService.getMaterialData().subscribe((materials) => {
this.materials = materials;
});
this.getFirebaseData();
}
onScroll() {
this.getFirebaseData();
}
private getFirebaseData(key?) {
if(this.finished) return;
this._materialDataFirebaseService.getData(this.group + 1, this.endKey)
.do(firebaseData => {
this.endKey = _.last(firebaseData)['$key']
const newData = _.slice(firebaseData, 0, this.group)
const currentData = this.firebaseData.getValue;
if(this.endKey == _.last(newData)['$key']) {
this.finished = true;
}
this.firebaseData.next(_.concat(currentData, newData));
})
.take(1)
.subscribe();
}
}
interface Material{
id: number,
name: string,
article_id: string,
tags: object,
state: string,
gallery_image: string
}
I did make sure that I imported everything properly and everything seems to work except I'm getting this Property 'do' does not exist on type 'void'.
in the component class.
The main idea of what I want to do is retrieving the data from firebase one batch at a time and load more on scroll event, to achieve this so called "Infinite Scroll" to reduce server load.