Today, I've been grappling with a challenging issue. As someone new to Typescript and Angular, I'm attempting to make a call to my backend API. However, when trying to populate an array for display, I keep encountering an error that says rawRegistrosList is undefined.
The error message I'm receiving is: "TypeError: rawRegistrosList.forEach is not a function at ConsultaService.buildConsultaList." Simply put, it's driving me slightly insane. And please excuse any mistakes in my English...
consulta.service.ts
private buildConsultaList(res: any){ // res has data when debugging
this.registros = [];
var rawRegistrosList: any[] = res.data;
rawRegistrosList.forEach(rawElement => { // error here, in rawRegistrosList.forEach
var registro : Consulta = new Consulta();
registro.cifEmpresa = rawElement.cifEmpresa;
registro.nombreEmpresa = rawElement.nombreEmpresa;
registro.ccc = rawElement.ccc;
registro.afiliado = rawElement.afiliado;
registro.nombreCompleto = rawElement.nombreCompleto;
registro.identTrab = rawElement.identTrab;
registro.altaMC = rawElement.altaMc;
this.registros.push(registro);
});
console.log("1 this.registro");
console.log(this.registros);
return this.registros;
}
consulta.json:
{ "data": { "idTrabajador": 1, "cifEmpresa": "20855585", "nombreEmpresa": "Poleomenta SL", "ccc": "08/254891/22", "nombre": "Pepito", "nombreCompleto": null, "primerApellido": "Grillo", "segundoApellido": "Grillado", "identTrab": null, "afiliado": "Si", "altMC": "No", "razonSocial": "Razón Social 1", "nifEmpresa": "5697413J", "direccion": "Calle desengaño 21", "localidad": "Alicante", "cp": "032120", "ubg": "", "tipoIpf": null, "ipf": null }, "backendError": null, "httpResponse": true, "totalCount": 0 }
I'm looking for some guidance on what's causing this error and if I'm initializing everything correctly. Any insights would be greatly appreciated.