1

I'm trying to display some info in the dom but I get this error:

Error: Error trying to diff '[object Object]'

What Im trying to do is iterate over this data from https://www.surbtc.com/api/v2/markets/btc-clp/ticker:

{"ticker":{"last_price":["1771455.0","CLP"],"min_ask":["1771432.0","CLP"],"max_bid":["1660003.0","CLP"],"volume":["178.37375119","BTC"],"price_variation_24h":"-0.107","price_variation_7d":"-0.115"}}

I want to display it in the html like this:

<div *ngFor="let price of prices">
    {{price.min_ask}}
    </div>

this is the service.ts:

import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import {Observable} from "rxjs";
import 'rxjs/Rx';
import 'rxjs/add/operator/catch';
import { SurbtcMarket } from './surbtcmarket'


@Injectable()
export class SurbtcService {

  constructor(private http: Http, private surBtcMarket : SurbtcMarket) { }

  public getPricess() :Observable<SurbtcMarket> {
    return this.http.get('https://www.surbtc.com/api/v2/markets/btc-clp/ticker')
    .map((response: Response) => response.json());
  }

}

Interface surbtcmarket.ts

export class SurbtcMarket {
public ticker: SurbtcMarketView[];
}

export class SurbtcMarketView {
  public last_price : number;
  public min_ask : number;
  public max_bid : number;
  public volume : number;
  public price_variation_24h : number;
  public price_variation_7d : number;
}

component.ts

import { Http, Response, Headers } from '@angular/http';
import { SurbtcService } from '../../surbtc/surbtc.service';
import {Observable} from "rxjs";

@Component({
  selector: 'app-comprarltc',
  templateUrl: './comprarltc.component.html',
  styleUrls: ['./comprarltc.component.scss']
})
export class ComprarltcComponent implements OnInit {

  private prices = [];

  constructor(private surbtcService: SurbtcService) {
    this.surbtcService = surbtcService;
  }

ngOnInit(){
  this.surbtcService.getPricess()
  .subscribe(
    data => this.prices = data.ticker
  );
}
}
user513951
  • 12,445
  • 7
  • 65
  • 82
claudiomatiasrg
  • 578
  • 4
  • 11
  • 31
  • Possible duplicate of [Error trying to diff '\[object Object\]'](https://stackoverflow.com/questions/38216857/error-trying-to-diff-object-object) – Nico Van Belle Jun 15 '17 at 18:17
  • 2
    The response you get from `https://www.surbtc.com/api/v2/markets/btc-clp/ticker` is not a JSON array but a JSON object (1 instance of `SurbtcMarket` in your case). You cannot use `*ngFor` on an Object because you cannot loop over it. – Nico Van Belle Jun 15 '17 at 18:20
  • @NicoVanBelle thanks for your reply. Is there a way to transform that object to an array? – claudiomatiasrg Jun 15 '17 at 18:22
  • Also, you mapped `ticker` as an array of type `SurbtcMarketView`but again, it's not an array, it is an object. – Nico Van Belle Jun 15 '17 at 18:23
  • 1
    Yes you can wrap your 1 object in an array, but you shouldn't do that. Just correct your mappings (check the JSON spec on object / array) and remove use the *ngFor loop. – Nico Van Belle Jun 15 '17 at 18:27
  • this is an 95% duplicate of your own question. (https://stackoverflow.com/questions/44552212/angular-4-object-object). Please stop polluting SO. Downvote it – Julian Jun 29 '17 at 16:31

3 Answers3

2

If you want to have price as an array, then you have push the object in prices array.

ngOnInit(){
  this.surbtcService.getPricess()
  .subscribe(
    data => this.prices.push(data.ticker);
  );
}

OR, you can just directly access the object properties by assigning data.ticker to prices.

private prices = new SurbtcMarketView();

constructor(private surbtcService: SurbtcService) {

}
ngOnInit(){
   this.surbtcService.getPricess()
        .subscribe(data =>{
            this.prices = data.ticker;
        });
}

HTML:

<div *ngFor="let item of prices.min_ask">
  {{item}}
</div>

UPDATE:

See the Plnkr demo, I have resolved the issue that was causing error in parsing the json response.

Nehal
  • 13,130
  • 4
  • 43
  • 59
  • Thanks for your reply @Nehal. Now I get a typing error saying that property ticker does not exist on type surbtcMarket[] – claudiomatiasrg Jun 15 '17 at 21:26
  • Added a working Plnkr demo, please see the updated answer. – Nehal Jun 16 '17 at 01:33
  • Thanks again @Nehal. Now I get this error in the console "Failed to load resource: the server responded with a status of 406 (Not Acceptable)". – claudiomatiasrg Jun 16 '17 at 18:04
  • That's a server error you have the resolve. Has nothing to do with angular. I was getting a CORS error for retrieving data from https://www.surbtc.com/api/v2/markets/btc-clp/ticker. So, I used the CORS chrome plugin to bypass the CORS error. – Nehal Jun 16 '17 at 18:39
0

Else you can also use this ,

prices : SurbtcService[] = [];

And then you can push the user object and make as a array,

this.prices.push(<<the data you want to push>>);
Mayur Saner
  • 444
  • 5
  • 10
-1
public getPricess() :Observable<SurbtcMarket> { return this.http.get('https://www.surbtc.com/api/v2/markets/btc-clp/ticker') .map((response: Response) => response.json() as SurbtcMarket); }

This might be due to the fact as it's not mapped to a type. And I assume this only retun one object otherwise you have to return an array.

public getPricess() :Observable<SurbtcMarket[]> { return this.http.get('https://www.surbtc.com/api/v2/markets/btc-clp/ticker') .map((response: Response) => response.json() as SurbtcMarket[]); }
Prav
  • 2,785
  • 1
  • 21
  • 30