[solved] Ionic 2 Pagination node js


#1

I’m trying to make paging with ionic 2 + nodejs.

But I have some problems.

I do the paging from node js and I get a JSON with the values ​​of each pagination.

NodeJS

function getPaginateUser(req, res) {
    let limite = 2;
    let page = (parseInt(req.params.num_page) - 1) * limite,
        num_page = parseInt(req.params.num_page),
        search = req.query.search,
        count;
    User
        .count()
        .then(date => {
            count = parseInt((date / limite) + 1);
        });
    User
        .find()
        .skip(page)
        .limit(limite)
        .then(user => {
            let context = {
                user: user,
                num_page: num_page,
                count: count
            }
            res.status(200).send(context)
            //res.render('user', context);
        });

I receive the data on a promise and subscribe them. But if I subscribe to it when I try to call back for page 2, http does not execute the promise. As I can call back the promise to return the new json with the data on page 2.

Ionic2 - provider

getUser2(num_page){
    if(this.data){
      return Promise.resolve(this.data);
    }
    return new Promise(resolve => {
      //this.http.get(this.path + '?results=0')
      this.http.get(this.path + num_page)
        .map(res => res.json())
        .subscribe(data => {
          this.data = data;
          resolve(this.data);
          
        });
        resolve(this.data);
    });
  }

Ionic 2 - ts

private LoadTimeline() {
    //.then((profile:any[]) =>{
    this.TimelineService.getUser(this.num_page)
      .then(timelines => {
        this.timeline = timelines;
      })
      .catch(error => {
        console.log(error);
      })
  }

#2

It looks to me like once getUser2 has been called once, it populates this.data and thus will simply follow the top code path forever thereafter. Why not just do this?

getUser2(numPage: number): Observable<User[]> {
  return this.http.get(this.path + numPage).map((rsp) => {
    return rsp.json();
  });
}

#3

Thank you.
I have solved my problem with:

getUser(num_page){
return this.http.get(this.path + num_page)
.toPromise()
.then(data => data.json());

} 

I’ll prove your code too. I see you keep the observable


#4

Yes. I don’t see any value in converting it to a promise.