Ionic Infinite Scroll End When No More Data Is Found

I am using json to data and what I want to do is I need to end infinite scroll once no more data is available.
How can I implement this.
Here is the code -

loadData(currentCount) {
    this.http.get('http://www.exapmle.com/feed.php?page=' + this.currentCount).map(res => res.json()).subscribe(data => {
        this.posts1 = data;
        let i = 0;
        for (let v in this.posts1) {
            if (i < 4) {
                this.slidesdata.push(this.posts1[v]);
            } else {
                this.posts.push(this.posts1[v]);
            }
            i++;
        }
    });
}

doInfinite(infiniteScroll: any) {
    this.currentCount = this.currentCount + 1;
    this.http.get('http://www.exapmle.com/feed.php?page=' + this.currentCount).map(res => res.json()).subscribe(data => {
        this.newposts1 = data;
        this.newposts = this.newposts1;
        for (let i in this.newposts) {
            this.posts.push(this.newposts[i]);
        }
        infiniteScroll.complete();
    });
}

I don’t know how you know when there is no more data, but however it is, you can set a flag in the controller and use it to wipe out the infinite scroll element with an *ngIf.

1 Like

I tried *ngIf, but I get the same error after there are no more pages to load data from.
Error - Unexpected token < in JSON at position 0

I solved this using LIMIT in my SQL Statement at ServerSide. And of course you have to use SORT BY

My IONIC App manage the actual index and send a limit to my server for example 40.

If the result array i get from my server back is less then 40 items i know ther ar no more records and I can disable the infinite scroll.

When I get you right your server send nor valid json when no more records are found so you could use your error handler to disable the infinite scroll

I am using pagination in API, it loads 10 rows at first and on scroll it loads 10 more result from page 2 and on and on.
But once no more pages are there, it throws this error. Do I use error handler in Ionic or in the query.?
And How to use error handler?

In ionic you can use try{} catch{} for error handling.

On the server side you coud send valid json even if there goes somthing wrong.

I send

{“status”:“ok”,“result”:[ARRAY]}

{“status”:“error”,“result”:“Error Text”}

Then I can react in my ionic app

if (response.status == “ok” then {

} else {

}

@Jacktoolsnet Thanks, this solved my issue…

I also used the server side method.