0

I am writing an angular website and I am new to the platform. I am trying to get data from a web service in JSON format then just output it. I have seen many examples where the data is first piped into an array of a class/model. But I would like to know if there is an easier way where I do not have to do this extra data structure. (This is a really simple application)

In my typescript class:

export class TestimonialsComponent implements OnInit {

  public testimonials: Observable<any[]>;
  baseUrl = 'http://127.0.0.1:80/';

  constructor(private httpClient: HttpClient){}

  ngOnInit() {
  }

  get_testimonials(){
      this.httpClient.get(this.baseUrl).subscribe((res : any[])=>{
      console.log(res);
      this.testimonials = res;
      });
  }

And in the HTML file

<button (click)="get_testimonials()">GET /products</button>



<li *ngFor='let testimonial of testimonials | async'>
  {{ testimonial.Name }}
</li>

The console successfully sees my object but I get this error message when I try to do the iteration:

ERROR Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'

3 Answers 3

1

The Error you are getting because of Async pipe

To know more about Async Pipe visit below link https://angular.io/api/common/AsyncPipe

Make Below changes in your code:

In typescript file:

 public testimonials: any[];

In Html file:

<li *ngFor='let testimonial of testimonials'>
    {{ testimonial.Name}}
</li>
Sign up to request clarification or add additional context in comments.

Comments

0

Or you could use your code without the async pipe.

<li *ngFor='let testimonial of testimonials'>
  {{ testimonial.Name }}
</li>

There are benefits and drawback in both approaches. I found that without sync I have more control on error handling and loading indicators. But I suspect it can be solved with async to. Tip: get_testimonials should be written getTestimonials

6 Comments

I get exactly the same error if I try this approach
NgFor only supports binding to Iterables such as Arrays. Check the data you get from the observable. console.log(res); Is it an array? Is it not set? You can also what data is transfered in f12 console. Is the array located in a property?
public testimonials: Observable<any[]>; should be public testimonials: any[]; as Ajit Nikam wrote
I think the array is located in the property data. try: testimonials.data
or better change this.testimonials = res.data (and then keep: let testimonial of testimonials)
|
0

enclose the testimonials | async into a parentheses which is much cleaner code

let testimonial of (testimonials | async)

and in the service do not subscribe to the http observable. the testimonials property should be an Observable.

get_testimonials(){this.testimonials = this.httpClient.get(this.baseUrl)};

the async will automatically subscribe and get the testimonials.

1 Comment

If I try this I now get this error: ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

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.