I have a binding {{getCategoryName(post.categories[0])['name']}} where I need to process the value post.categories[0] returned in the *ngFor loop with a method getCategoryByName() in my component before finally binding it to the template.The method returns an Object.
I keep getting the error Cannot read property 'name' of undefined . I have tried getting the property using the ['name'] key instead of .name but still get the error.
Any thoughts for a workaround?
<ion-item text-wrap class="news-item" *ngFor="let post of posts" (click)="viewStory(post)">
<div >
<p>{{getCategoryName(post.categories[0])['name']}}</p>
<h2>{{post.title.rendered}}</h2>
<p>By Fitz · {{post.date | fromNow}}</p>
</div>
</ion-item>
The method
getCategoryName(categoryId) {
return this.categories[this.categories.findIndex((el)=>{ return el.id == categoryId})];
}
Example ember of the Array from which the Object is returned by method above
[
{
"id": 33,
"count": 1,
"description": "",
"link": "http://XXXXXXXXXXXXX.co/category/apps/",
"name": "Apps",
"slug": "apps",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "http://XXXXXXXXXXXXX.co/wp-json/wp/v2/categories/33"
}
],
"collection": [
{
"href": "http://XXXXXXXXXXXXX.co/wp-json/wp/v2/categories"
}
],
"about": [
{
"href": "http://XXXXXXXXXXXXX.co/wp-json/wp/v2/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "http://XXXXXXXXXXXXX.co/wp-json/wp/v2/posts?categories=33"
},
{
"href": "http://XXXXXXXXXXXXX.co/wp-json/wp/v2/jobs?categories=33"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
]
getCategoryNameand the structure ofpostsarray