1

folks, I am working on a project that needs to filter JSON but in developer tools of Chrome, it shows me an error of undefined property.

 chart: JsonChart[] = [];
 charts: JsonCharts[] = [];

 getCharts() {
    this.iChartHttp.getCharts()
        .subscribe(
        charts => this.charts = charts, //return json object
        error => this.errorMessage = <any>error
        );
}

 if (this.chart_id != null) {
        this.getCharts();
         this.chart = this.charts.filter(charts => charts.id === this.chart_id)[0].series; // this like error occur.
    }

Updated Question

It's not getting JSON Array from Service. but it works in another component

iChartHttpService:

 getCharts(): Observable<JsonCharts[]> {
return this.http.get('assets/datas.json')
  .map((res: Response) => res.json())
  .do(data => console.log('server data:', data))  // debug
  .catch(this.handleError);
}

/**
* Handle HTTP error
*/
private handleError(error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
const errMsg = (error.message) ? error.message :
  error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);

}

2 Answers 2

2

It occurs when charts is null, try to place your filter logic after the results,

  getCharts() {
        this.iChartHttp.getCharts()
            .subscribe((charts: any) => {
            this.charts = charts, //return json object
            this.chart = this.charts.filter(charts => charts.id === this.chart_id)[0].series; 
            }, error => {
                error => this.errorMessage = <any>error
            });
    }
Sign up to request clarification or add additional context in comments.

10 Comments

why charts getting null?
@ Sajeetharan I didn't get that, the this.charts is declared in outside of getcharts() method so it may not be getting null after the subscribe
@Sajeedtharan Yes but it gets error: Argument of type 'JsonChart[]' is not assignable to parameter of type '(error: any) => void'. Type 'JsonChart[]' provides no match for the signature '(error: any): void'.
it seems it didn't pass any JSON object, but in service, it's getting JSON object from API
not it's not :) but I see the method didn't return any JSON object from iChartHttp.getCharts(). But in iChartHttp.getCharts() it's logs JSON array successfully
|
1

I think you need to send the series inside the javascript filter function. modify it like this

if (this.chart_id != null) {
    this.getCharts();
    if (this.chart.length > 0) {
        this.chart = this.charts.filter(charts => {
            if (charts.id === this.chart_id) {
                charts[0].series;
            }
        })
    }
}

1 Comment

thank you sachila but the this.charts is getting null

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.