arraysangularngfor

Error trying to diff '[object Object]'. Only arrays and iterables are allowed but i dont see anything wrong with my code


I have been having this problem for the past week, I have searched everywhere but wasn't able to find a problem.

My service

private texst!: Posts[];

  
  public getPosts(): Observable<Posts[]>
  { 

    return this.http.get<Posts[]>("http://localhost/projects/php_rest_api/api/post/read.php").pipe(map((data) => {
      return this.texst = data;
    }));
  }

My Component, here i add the service and run the function to get the data from my database

public test: Posts[] = [];]

  constructor(public postService: PostsService,
              private http: HttpClient) {}

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

  public getPosts()
  {
    this.postService.getPosts().subscribe((response) => {
      this.test = response;
      console.log(this.test);
    })
  }

My html

<div>
    <button (click)="getPosts()"></button>
    <div *ngFor="let test of test">
        {{test.title}}
    </div>
</div>

enter image description here


Solution

  • Managed to fix it

    changed the response to object.values in my getPosts() function

     public getPosts()
      {
        this.postService.getPosts().subscribe((response) => {
          this.test = Object.values(response);
          console.log(this.test);
        })
      }