I am trying to unite Angular with Spring Boot but I have problems and it is not loading my data from my database in my html, it is throwing me this error in my front-end, I am using CORS ; I implemented the listUser service in the Spring part and I want to show it in a table; What mistake am I making? Thanks for your help I would appreciate it.
ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
at NgForOf.ngDoCheck (common.js:3193)
at callHook (core.js:3285)
at callHooks (core.js:3251)
at executeCheckHooks (core.js:3184)
at refreshView (core.js:7335)
at refreshComponent (core.js:8473)
at refreshChildComponents (core.js:7132)
at refreshView (core.js:7376)
at refreshEmbeddedViews (core.js:8427)
at refreshView (core.js:7350)
this is the code in spring, there is the Angular route and the main route which is the RequestMapping and the list route
@RestController
@CrossOrigin(origins = {"http://localhost:4200"})
@RequestMapping("/api/usu")
public class UsuarioRestControlador {
@Autowired
private UsuarioServicio usuarioServicio;
@GetMapping("/listarUsuario")
public Map<String,Object> listarUsuario(){
Map<String,Object> rpta= new HashMap<String,Object>();
List<Usuario> listaUsuario = usuarioServicio.listarUsuario();
rpta.put("listado",listaUsuario);
return rpta;
}
this is the code in angular: this is the service, this is the path of my localhost , I am also importing my User class
import {Injectable} from '@angular/core';
import {USUARIO} from '../usuario/usuario.json';
import {Usuario} from './usuario'
import {Observable} from 'rxjs';
import {of} from 'rxjs';
import {HttpClient} from '@angular/common/http';
@Injectable()
export class UsuarioService{
private url : string = 'http://localhost:8080/api/usu/listarUsuario';
constructor(private http:HttpClient){
}
getUsuarios():Observable<Usuario[]>{
return this.http.get<Usuario[]>(this.url);
}
}
this is the Json that returns in Spring
{"listado":[{"fec_REG_USUARIO":"2020-10-22T11:38:14.383","contra_USUARIO":"1234","usu_USUARIO":"local2","nom_USUARIO":"carlos aldair","fec_NAC":null,"id_USUARIO":2,"apell_USUARIO":"zavala"},{"fec_REG_USUARIO":"2020-10-26T13:52:35.253","contra_USUARIO":"1234","usu_USUARIO":"local2","nom_USUARIO":"juan","fec_NAC":null,"id_USUARIO":3,"apell_USUARIO":"zavala tomayro"},{"fec_REG_USUARIO":"2020-10-26T13:53:20.477","contra_USUARIO":"123","usu_USUARIO":"local2","nom_USUARIO":"matias","fec_NAC":null,"id_USUARIO":4,"apell_USUARIO":"tomayro"},{"fec_REG_USUARIO":"2020-10-26T15:27:07.103","contra_USUARIO":"123","usu_USUARIO":"local2","nom_USUARIO":"carlos","fec_NAC":null,"id_USUARIO":5,"apell_USUARIO":"tomayro"},{"fec_REG_USUARIO":"2020-10-26T15:57:55.697","contra_USUARIO":"contrasena","usu_USUARIO":"usuario","nom_USUARIO":"nombre","fec_NAC":null,"id_USUARIO":6,"apell_USUARIO":"apellido"},{"fec_REG_USUARIO":"2020-10-26T16:04:21.93","contra_USUARIO":"123","usu_USUARIO":"123","nom_USUARIO":"mati","fec_NAC":null,"id_USUARIO":7,"apell_USUARIO":"casas"},{"fec_REG_USUARIO":"2020-10-26T16:05:36.33","contra_USUARIO":"123","usu_USUARIO":"123","nom_USUARIO":"cristian","fec_NAC":null,"id_USUARIO":8,"apell_USUARIO":"quispe"},{"fec_REG_USUARIO":"2020-10-26T16:06:35.517","contra_USUARIO":"123","usu_USUARIO":"123","nom_USUARIO":"manolo","fec_NAC":null,"id_USUARIO":9,"apell_USUARIO":"cazt"},{"fec_REG_USUARIO":"2020-10-27T11:34:56.797","contra_USUARIO":"1234","usu_USUARIO":"1234","nom_USUARIO":"juan carlos","fec_NAC":null,"id_USUARIO":12,"apell_USUARIO":"carballo"},{"fec_REG_USUARIO":"2020-10-27T16:49:44.233","contra_USUARIO":"8778","usu_USUARIO":"54+","nom_USUARIO":"guty","fec_NAC":null,"id_USUARIO":13,"apell_USUARIO":"quispe arias"},{"fec_REG_USUARIO":"2020-10-30T19:08:53.773","contra_USUARIO":"48878","usu_USUARIO":"yayito","nom_USUARIO":"yayito lucas","fec_NAC":"2017-06-13T05:00:00.000+00:00","id_USUARIO":15,"apell_USUARIO":"husto"},{"fec_REG_USUARIO":"2020-11-10T16:19:01.49","contra_USUARIO":"1234","usu_USUARIO":"local2","nom_USUARIO":"aldair","fec_NAC":"0024-12-15T05:00:00.000+00:00","id_USUARIO":16,"apell_USUARIO":"zavala"},{"fec_REG_USUARIO":"2020-11-10T16:19:45.577","contra_USUARIO":"1234","usu_USUARIO":"local2","nom_USUARIO":"aldair","fec_NAC":"0024-12-15T05:00:00.000+00:00","id_USUARIO":17,"apell_USUARIO":"zavala"},{"fec_REG_USUARIO":"2020-11-10T16:20:02.04","contra_USUARIO":"1234","usu_USUARIO":"local2","nom_USUARIO":"pepito","fec_NAC":"0024-12-15T05:00:00.000+00:00","id_USUARIO":18,"apell_USUARIO":"casas"},{"fec_REG_USUARIO":"2020-11-10T16:20:51.72","contra_USUARIO":"1234","usu_USUARIO":"local2","nom_USUARIO":"patito","fec_NAC":"1995-07-19T05:00:00.000+00:00","id_USUARIO":19,"apell_USUARIO":"casas"}]}
here I receive the service
import { Component, OnInit } from '@angular/core';
import {Usuario} from '../usuario/usuario';
import {UsuarioService} from '../usuario/usuario.service';
@Component({
selector: 'app-listar',
templateUrl: './listar.component.html',
styleUrls: ['./listar.component.css']
})
export class ListarComponent implements OnInit {
usuarios:Usuario[];
constructor(private usuarioService:UsuarioService) { }
ngOnInit(): void {
this.usuarioService.getUsuarios().subscribe(
usuarios =>this.usuarios = usuarios
);
}
}
this is the html
<div class="container">
<table class="table table-bordered table-striped contenedor">
<thead>
<tr>
<th>nombre</th>
<th>apellido</th>
<th>usuario</th>
<th>contaseña</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let usuario of usuarios">
<td>{{usuario.nombre}}</td>
<td>{{usuario.apellido}}</td>
<td>{{usuario.usuario}}</td>
<td>{{usuario.contra}}</td>
</tr>
</tbody>
</table>
</div>
The list of users returned by the backend is in a property called
listado
:In the Angular service you have to access that property:
Update