0

enter image description here

the data im getting from server comes in this format i want it to come in a form of an Object.

public getOrder(): Observable < ORDERS > {
  return this._http.get < ORDERS > (`${this._apiBase}/charts/list/ORDERS/`);
}

here how im getting the data from server.

ngOnInit() {
  this._dashService.getOrder().subscribe(order => {
    this.orders = order;
    console.log(this.orders);
  })
}

2
  • Is the issue that the response you get is a string rather than an object? Commented Oct 2, 2018 at 9:32
  • yes that the issue Commented Oct 2, 2018 at 9:40

3 Answers 3

2

This probably means that the your service is not setting the Content-Type property of the http header to application/json.

If you own the service, you could try to set the header property of the response. If you are not able to change it, you can try the JSON.parse() method.

Sign up to request clarification or add additional context in comments.

5 Comments

this works but it requires me to replace Observable<ORDERS> with Observable<any>
you can cast it in typescript: let result = this._http.get < ORDERS > ('${this._apiBase}/charts/list/ORDERS/'); return JSON.parse(result) as ORDERS; I am getting the feeling that your format is not correct. Are you sure the result from the service can map to the ORDERS type?
@muzijack What does ORDERS look like?
export interface ORDERS { dc: number; name: string; type: string; category: string; dc_id: string; dc_name: string; series: Array<Series>; }
I think you should use ORDERS[] instead of ORDER Your data contains multiple ORDER sets. Make the return type Observable<ORDERS[]> and you should be fine. You can even drop the JSON.parse() code.
0

This will do the trick!

public getOrder(): Observable < ORDERS > {
  return this._http.get(`${this._apiBase}/charts/list/ORDERS/`).map((res) => <ORDERS>res.json())
}

3 Comments

this gives an error map function is not provided in this response only subscribe
@muzijack did you import 'rxjs/add/operator/map' ?
0

Just convert your response using json method.

public getOrder(): Observable < ORDERS > {
  return this._http.get(`${this._apiBase}/charts/list/ORDERS/`)
                   .map((res) =>res.results as ORDERS[] || [])
                   .catch((error:any) => Observable.throw(error.json().error));
}

4 Comments

Property 'map' does not exist on type 'Observable<Object>'.
you have to import operator from rxjs.add following import to your code...import { map } from 'rxjs/operators';
Property 'json' does not exist on type 'Object'
@AravindaMeewalaarachchi there is typo in your post ! res.json() not response.json()

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.