1
votes

I am trying to implement an infinite scroll in Angular 2 and retrieving data from firebase. I am using an Observable but that is bringing all of the records I have in the database, the code for it is

getUsers(): Observable<any> {
    return this.http.get(`${baseUrl}.json`).map((res: Response) => {
    this.data = res.json();
    return this.data;
    });
}

for making bringing the data in chuncks I tried one of the Angular 1 implementations from the link

https://stackoverflow.com/a/40634381/5290012

and also tried implementing

https://medium.com/@Sureshkumar_Ash/angular-2-simple-infinite-scroller-directive-with-rxjs-observables-a989b12d4fb1

using either is throwing errors saying that the properties do not exist on the

Property 'orderByChild' does not exist on type 'Observable'.

please suggest how I need to approach

1
Are you trying to use angularfire2? Because what you're doing doesn't make use of angularfire2 at all. I'd go over to the angularfire2 repo and follow the docs. orderByChild is usable only on AngularfireDatabase lists. - J. Adam Connor
@J.AdamConnor Thank you very much, you are right my implementation was wrong. - Vijay
Yeah, you're going to have to paginate your results with the query functionality, changing the endAt value dynamically while you scroll. Something along those lines.... - J. Adam Connor
@J.AdamConnor yes you are right, I am able to fetch limited data for now, I do not think it would be a hard implementation, will update a fiddle once I do implement the same. Thank you for your time. - Vijay

1 Answers

1
votes

I was able to implement this by splitting the function in to two to begin with, and the addItems() function is called each time the scroll reaches the bottom of the page, which fetches additional data.

    getUsers(): Observable<any> {
    return this.fb.list('/items', {
      query: {
        orderByChild: 'pnr_id',
        limitToFirst: 200,
      }
    });
  }

  addItems(newStart) {
    console.log(newStart);
      return this.fb.list('/items', {
        query: {
          orderByChild: 'pnr_id',
          startAt: newStart,
          endAt: newStart + 200,
        }
      });
    };

That is working flawlessly for me now.