My service retrieves data from an endpoint :
Service.ts
getAllProducts(){
return new Promise( (resolve, reject) => {
this.apiService.get( this.allProducts, `/products`, {} )
.then( data => resolve( data.map( item => this.parseProductDetails( item ) ) ) )
.catch( err => reject( err ) );
} );
}
console.log(data)
- shows all products. The service is working fine and the function this.parseProductDetails also returns products correctly.
However, when I call it from the component :
ionViwDidLoad(){
this.productProvider.getAllProducts()
.then( () => {
items => this.items = items;
console.log('All products', this.items)
} )
.catch( err => console.debug( 'products not found', err ) )
}
console.log('All products', this.items)
- nothing is returned in the console log, not even undefined or the 'All products' text.
What is wrong with this code and what do I need to change in order to retrieve the information in the component?
parseProductDetails(item) :
protected parseProductDetails( data: any ): Object {
let parsed: any = data;
try {
parsed.dimensions = JSON.parse( data.dimensions );
} catch( e ) { parsed.dimensions = []; }
if( data.price )
parsed.priceFormatted = this.formatPrice( data.price, data.currency );
else
parsed.priceFormatted = false;
if( data.delivery )
parsed.deliveryFormatted = this.formatPrice( data.delivery, data.currency );
else
parsed.deliveryFormatted = false;
if( data.stock )
parsed.stockFormatted = this.formatStock( data.stock, data.stockUnit );
else
parsed.stockFormatted = false;
return parsed;
}
returning parsed as an array.