-2

I am building app which shows amazon products :

showProducts(){
    this.search.getProducts().subscribe
    ((data: any) => {
        this.list = data['hydra:member'];
        this.prices = data.lowestPrices.Amazon.newItem;
        console.log('Objects', this.list);
        console.log('Prices', this.prices);
    });

}

this.list in console :

enter image description here

this.prices in console :

Cannot read property 'Amazon' of undefined

How to get lowest prices of Amazon properly ?

daedsidog
  • 1,732
  • 2
  • 17
  • 36
Angulandy2
  • 786
  • 4
  • 13
  • 31

2 Answers2

2

You're accessing an array []. Arrays have indexes [0]->[n]

In your example it would be

showProducts(){
 this.search.getProducts().subscribe
 ((data: any) => {
  this.list = data['hydra:member'];
  this.prices = data['hydra:member'][0].lowestPrices.Amazon.newItem;
  console.log('Objects', this.list);
  console.log('Prices', this.prices);
});

for the first item.

You'll have to write a loop for each item of data for every information

Nikolai Kiefer
  • 568
  • 5
  • 15
1

Use Array map, to iterate over the array and populate your properties For Example:

this.prices = data['hydra:member'].map(elem => elem.lowestPrices.Amazon.newItem;);
Ashish Ranjan
  • 12,760
  • 5
  • 27
  • 51