Why am I encountering an error and what steps can be taken to resolve it? Currently using the latest version of Angular.
ERROR TypeError: Cannot read properties of undefined (reading 'id')
Here is the JSON data:
{
"settings": [
{
"test": "test",
"title": "Service 1"
}
],
"services": [
{
"id": "1",
"title": "Service 1"
},
{
"id": "2",
"title": "Service 2"
}
]
}
The code snippet below is from app.component.html:
<p>service id {{ service.id }}</p>
Below is the section of app.component.ts where the issue might lie:
ngOnInit(): void {
// Retrieve data
this.api.getApi()
.subscribe(res => {
this.data = res;
// Get specific service
this.service = this.data.services.filter((obj: any) => {
return obj.id === this.id;
});
});
}