Error: NG0900: Error trying to diff '[object Object]'. Only arrays and iterables are allowed.
Me esta saliendo este error en la consola al hacer una petición get y he estado comprobando todo el código y no se que puede ser lo que me ha provocado el fallo, porque es un array y me coge además los datos del back como se puede ver aquí:
import { Component } from '@angular/core';
import { Code } from '../../model/code';
import { codeService } from '../../_services/codeService';
import { ActivatedRoute, Router } from '@angular/router';
import Swal from 'sweetalert2';
@Component({
selector: 'app-code',
templateUrl: './code.component.html',
styleUrls: ['./code.component.css']
})
export class CodeComponent {
codes: any[]=[];
idProject: number;
constructor(private codeService: codeService, private router:Router, private route: ActivatedRoute){}
ngOnInit(): void {
this.getCodes();
}
private getCodes(){
this.codeService.getAll().subscribe((dato) => {
this.codes = dato;
console.log(dato)
if(this.codes.length == 0){
Swal.fire({
title: 'No existen codes ',
text: 'Crea un code para que aparezca en la pantalla',
icon: 'info'
});
}
});
}
eliminarCode(idCode:number){
this.codeService.delete(idCode).subscribe(() =>{
this.getCodes();
Swal.fire({
title: 'Code eliminado correctamente',
icon: 'success'
});
});
}
navegarCrearCode(){
this.router.navigate(['code/']);
}
navegarEditarCode(idCode:number){
this.router.navigate(['code/', this.idProject,idCode]);
}
}
la clase con las propiedades:
export class Code {
id: number;
paquete: string;
file: string;
}
El servicio:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, tap } from 'rxjs';
import { Code } from '../model/code';
const baseUrl = 'http://localhost:8080/api/code';
@Injectable({
providedIn: 'root'
})
export class codeService{
constructor(private http: HttpClient) { }
getAll():Observable<Code[]> {
return this.http.get<Code[]>(`${baseUrl}`).pipe(
tap(res => console.log(res))
);
}
get(id: any): Observable<Code> {
return this.http.get<Code>(`${baseUrl}/${id}`);
}
create(data: any): Observable<any> {
return this.http.post(baseUrl, data);
}
update(id: any, data: any): Observable<any> {
return this.http.put(`${baseUrl}/${id}`, data);
}
delete(id: any): Observable<any> {
return this.http.delete(`${baseUrl}/${id}`);
}
deleteAll(): Observable<any> {
return this.http.delete(baseUrl);
}
}
Mi html:
<div class="container">
<h3 class="text-center">Lista de Codes </h3>
<div class="row">
<div class="col">
<table class="table table-striped">
<thead>
<tr>
<th style="color: blue;font-weight: bolder;">Id</th>
<th style="color: green; font-weight: lighter;">Paquete</th>
<th style="color: green; font-weight: lighter;">File</th>
<th style="color: green; font-weight: lighter;">Operaciones</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let code of codes">
<td>{{code.id}}</td>
<td>{{code.paquete}}</td>
<td>{{code.file}}</td>
<th>
<div class="btn-group" role="group">
<input class="btn btn-secondary" type="submit" value="Editar" (click)="navegarEditarCode(code.id)">
<input class="btn btn-danger" type="submit" value="Eliminar" (click)="eliminarCode(code.id)">
</div>
</th>
</tr>
<div *ngIf="!codes || codes.length === 0">
No hay datos para mostrar.
</div>
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col d-flex justify-content-center">
<a class="btn btn-success" type="submit" (click)="navegarCrearCode()">Crear Code</a>
</div>
</div>
<br>
<div class="row">
<div class="col d-flex justify-content-center">
<a class="btn btn-secondary" type="submit" routerLink="/home">Volver</a>
</div>
</div>
</div>

