0

I've start learning angular 2. I try to get some data via http get request and then I would like to build objects with that data so I can display them with template later. If I am thinking with the wrong way you can tell me.

I have my model AnalyticsData:

export class AnalyticsData {
     pagePath: string;
     pageViews: number;
     uniquePageViews: number;
     avgTimeOnPage: number;
     entrances: number;
     bounceRate: number;

    constructor(object?: any) {
        this.pagePath = object && object.pagePath || null;
        this.pageViews = object && object.pageViews || null;
        this.uniquePageViews = object && object.uniquePageViews || null;
        this.avgTimeOnPage = object && object.avgTimeOnPage || null;
        this.entrances = object && object.entrances || null;
        this.bounceRate = object && object.bounceRate || null;
    }

}

My DataService:

export class DataService {

    private dataUrl: string = 'http://example.com/app/analyticsdata';
    constructor(private http: Http) { }

    getData() {
        return this.http.get(this.dataUrl)
            .map((response: Response) => response.json());
    }

}

My AnalyticsComponent:

export class AnalyticsComponent implements OnInit {

    myData: Array<AnalyticsData>;

    constructor(private services: DataService) { }

    ngOnInit(): void {
        this.getData();
    }

    getData() {
        this.services.getData()
            .subscribe(
            function (response) {
                response.forEach((element: AnalyticsData, index: number) => {
                    this.myData.push(
                        new AnalyticsData({
                            pagePath: element['ga:pagePath'],
                            pageViews: element.pageViews,
                            uniquePageViews: element.uniquePageViews,
                            avgTimeOnPage: element.avgTimeOnPage,
                            entrances: element.entrances,
                            bounceRate: element.bounceRate
                        })
                    );
                });
            },
            function (error) { console.log("Error happened" + error) },
            function () {
                console.log("the subscription is completed");
            }
            );

    }

}

The error with the above is: EXCEPTION: Cannot read property 'push' of undefined. I don't understand why this happened because I've assign the variable myData on the top of the class.

3
  • myData: Array<AnalyticsData[]>; Commented Oct 11, 2016 at 5:54
  • Add this to your constructor and try again: this.MyData = []; Commented Oct 11, 2016 at 5:54
  • @HarryNinh Thank you I combine your answer with micronyks answer to solve my problem. Commented Oct 11, 2016 at 6:15

1 Answer 1

1

Also use arrowFunction ()=> as shown below,

getData() {
        this.services.getData()
            .subscribe(
            (response) => {                                       //<<<<===here
                response.forEach((element: AnalyticsData, index: number) => {
                    this.myData.push(
                        new AnalyticsData({
                            pagePath: element['ga:pagePath'],
                            pageViews: element.pageViews,
                            uniquePageViews: element.uniquePageViews,
                            avgTimeOnPage: element.avgTimeOnPage,
                            entrances: element.entrances,
                            bounceRate: element.bounceRate
                        })
                    );
                });
            },
            (error) => { console.log("Error happened" + error) }, //<<<===here
            () => {                                               //<<<===here
                console.log("the subscription is completed");
            }
            );

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

1 Comment

Thank you. I also need to put the this.myData = []; in contractor as @HarryNinh say

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.