7

I have a model:

export class CoreGoalModel {
  constructor(

    public title: string,
    public image: string, 

    ){}
}

My service:

  getGoals(): Observable<CoreGoalModel[]> {

    let headers = new Headers({ 'Access-Control-Allow-Origin': '*' });
    let options = new RequestOptions({ headers: headers });

    return this.http.get(this.base_url + 'coregoal', options)
    .map(this.extractData)
    .catch(this.handleError);
  }

  private extractData(res: Response) {
    let body = res.json();
    return body.data || { };
  }

And then in my component:

ngOnInit() {

    this.homeService.getGoals()
    .subscribe(
                 goals => this.coreGoals = goals,
                 error =>  this.errorMessage = <any>error);

}

I then am binding this in my template as:

<ul>
    <li *ngFor="let goal of coreGoals">
        {{goal.title}}
    </li>
</ul>

My actual response from server:

[{"coreGoalId":1,"title":"Core goal 1","infrastructure":"Sample Infrastructure","audience":"People","subGoals":null,"benefits":[{"benefitId":1,"what":"string","coreGoalId":1}],"effects":null,"steps":null,"images":[{"imagelId":1,"base64":"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcU\nFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgo\nKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wgARCAIWAe4DASIA\nAhEBAxEB/8QAHAABAAIDAQEB"}]}]

This throws me error Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

What am I doing wrong? I simply would like to iterate over coreGoals properties and also access it's children and their properties.

1
  • Upgrade to 4 and it's somewhat more flexible now. Commented Mar 31, 2017 at 10:20

1 Answer 1

9

Your error is here:

private extractData(res: Response) {
  let body = res.json();
  return body.data || { }; // error
}

Your response has no object named data, so remove data and it should work:

private extractData(res: Response) {
  let body = res.json();
  return body || { }; // here
}
Sign up to request clarification or add additional context in comments.

5 Comments

Can you please also point out how can I access it's children like in images -> base64 so that I can see the image on UI?
I figured it out :)
Just about to answer, but you figured it out, great! :) Have a good day and happy coding!
Thanks a lot! But I still ran into error = > <img [src]="'data:image/jpg;base64,'+ goal.images[0].base64 " /> is it a correct way in my loop?
I didn't notice that your image was of base64. Yeah, that looks about right though. But you will run to an "unsafe" error right? You need to check other SO posts about that... maybe this can help? stackoverflow.com/questions/38812993/base64-to-image-angular-2

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.