0

I can't seem to figure out what I'm doing wrong. I"m trying *ngFor on a typed Market[] variable, but for some reason it can't read the array properly. Ionic is complaining markets is not a array, but from my implementation it would appear it is. Help

Market def:

 export interface Market {
    id: string;
    symbol: string;
    base: string;
    quote: string;
    info: any;
    lot: number;
}

My Service call

  getMarketData(): Observable<Market[]> {
    let bittrex = new ccxt.poloniex();
    return Observable.fromPromise(bittrex.loadMarkets());
  }

My page setting the data

    markets: Market[] = [];
    async loadMarkets() {
      this.cryptoService.getMarketData().subscribe((data: Market[]) => {
      this.markets = data;
    })
  }

My template

<ion-list #scheduleList [hidden]="markets.length < 0">

<ion-item-group *ngFor="let market of markets">

  <ion-item-divider sticky>
    <ion-label>
      {{market}}
    </ion-label>
  </ion-item-divider>
</ion-item-group>

What I don't understand is why I'm not getting back a array.

error

12
  • you seem to be missing } here: {{market}..not sure if it is related though Commented Dec 21, 2017 at 7:03
  • Good catch. Fixed it, but alas still the same issue Commented Dec 21, 2017 at 7:04
  • what do you want to show inside of <ion-label> ? Commented Dec 21, 2017 at 7:05
  • for now I don't care just want it read the array properly. I've tried without it with same issue Commented Dec 21, 2017 at 7:06
  • did you console log data in subscribe and verify if it is an array? Commented Dec 21, 2017 at 7:06

2 Answers 2

1

Check *ngIf before *ngFor try this

<div *ngIf="markets">
<ion-item-group *ngFor="let market of markets">

  <ion-item-divider sticky>
    <ion-label>
      {{market}}
    </ion-label>
  </ion-item-divider>
</ion-item-group>
</div>

And check your response data instance is an array | correct type

this.markets=[]
if (data instanceof Array) {
      this.markets = data;
  }
Sign up to request clarification or add additional context in comments.

2 Comments

These fixed the error. However nothing appears on the page (
Because your data format is not an array... Ngfor should accept array of data formats only... You should check with your data....
1

So basically my return object was not a true array. Even though Typescript was returning Market[] array . The result was a object of Market. The trick was.

   getMarketData(): Observable<Market[]> {
    let bittrex = new ccxt.poloniex();
    return Observable.fromPromise(bittrex.loadMarkets()).map(data => 
    Object.keys(data).map(k => data[k]))
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.