Problems with infinite scroll


#1

I appeal to you because I have a problem with an infinite scroll, I managed to implement one for all products that is ok, but when implementing one for a product by category type (the category receives the type) I can not get it to work or it loads 10 to me and it repeats them to me again that with the answer of the servant I assigned it to an arrangement.
But if I make a .push to the arrangement with what comes in the data it works excellent but to the categories that have less than 10 products it tells me that the array is empty.
Sorry to bother you but it took several weeks and I do not succeed.
Thanks in advance

my services

cargaPorCategoria ( categoria: number ) {
this.tipCategoria = categoria;

let promesa = new Promise ( (resolve, reject)=>{
  //let url = `${ URL_SERVICIOS }/registro/obtenerPorTipo/${ categoria }`;
  let url = URL_SERVICIOS + "/productos/obtenerPorTipo/" + categoria + "/" + this.pagina; //Obtengo el url

  //Peticion del servicio
  this.http.get( url ).subscribe ( res => {
    //console.log(res);
    if ( res['error'] ) {
      //Aquí tenemos un error
    }
    else{
      //Creamos nueva data le pasamos los productos y el ancho que la queremos en este caso sería en 2 columnas
      //let nuevaData = this.agrupar( res['productos'], 2 );
      //this.Porcategorias = this.Porcategorias.concat(...res['productosPorCategoria']);;
      this.Porcategorias.push(...res['productosPorCategoria']);//Aqui tendría que reemplazar "res['productos']" por la "nuevaData" para que se dibujaran en 2 columnas
      console.log("Este es...")
      console.log(this.Porcategorias)
      console.log(this.pagina)
    }
    this.pagina += 1;
    //this.pagina ++;
  })  
  resolve();

})
return promesa;

}

my code

/******************************* Inicio Scroll infinito **************/
productoscroll( infiniteScroll ) {
this.prodProv.cargaPorCategoria( this.categoria.idCat )
.then( ()=>{
setTimeout(() => {
infiniteScroll.complete();}, 800);
} )
}
/
Fin Scroll infinito *********************************************/


#2

Don’t create a new Promise like that. It’s an antipattern. if you see tutorials recommending that, those tutorials are lying to you. Use this structure.

servicePetition(): Observable<Array<Product>> {
   return this.http.get(url)
                   .pipe(switchMap(response => return { if (response is an error) return Observable.from(product errorflag)
                                                                              else return whateverProductObservableYouWant }
}

Don’t use setTimeout for any of this. It will break on the wrong device. Instead of switchMap, you can use mergeMap, flatMap, or other operators, depending on what you want to do.


#3

Thanks, Aaron.
but I do not understand what should I do with the code that you are passing me?
I’m sorry I’m new and it’s my first robust job in ionic 3


#4