Encountering an ERROR TypeError:
Cannot read property 'id' of undefined
while attempting to delete data in a modal using Bootstrap. When I click the button, I correctly receive the data in JSON format in the modal (as shown in the image). Any ideas on how to resolve this issue?...
By the way, the insert, select, and update functionalities work as intended, but the delete operation is throwing this error.
component.ts
selectedExamen;
examenes: Examen[];
@ViewChild('btnClose') btnClose: ElementRef;
constructor(private dataService: dataService, public dialog: MatDialog) {
this.getExamenes();
this.selectedExamen = {
id: -1, nombreExa: '', descripcionExa: '',
release_date: '', fechaExa: '', categoriaExa: '', nombrePac: '',
apellidoPac: '', rutPac: '', apellidoDoc: '', nombreDoc: ''
};
}
delete (exa): void {
this.dataService.deleteExamenes(exa.id);
this.examenes = this.examenes.filter(e => e !== exa);
}
examenClicked = (exa) => {
this.dataService.getOneExamen(exa.id).subscribe(
(data: Examen) => {
this.selectedExamen = data;
console.log('JSON DATA --->', data);
},
error => {
alert('Error al Conectar Datos ');
}
);
}
component.html
<button class="btn btn-danger" href="#deleteCategory" (click)="examenClicked(categorias)" data-toggle="modal" >Borrar</button>
<button class="btn btn-secondary" type="button" href="#signinModal" data-toggle="modal" (click)="examenClicked(categorias)">Editar</button>
"modal button"
<button type="button" class="btn btn-danger" (click)="delete()">Borrar</button>
service (I suspect the mistake lies here?)
deleteCategorias(id: number): Promise <void> {
const url = `${"http://127.0.0.1:8000/categoria"}/${id}`;
return this.http.delete(url, { headers: this.headers })
.toPromise()
.then(() => null)
}
getOneCategory(id): Observable <Categoria> {
return this.httpClient.get<Categoria>(this.baseurl + '/categoria/' + id ,
).pipe()
}
The error occurs when I press the button in the modalhttps://i.sstatic.net/8ApKM.png