嘿,我有两个字段的content_type类别-标题和subcategory.Sub类别是引用选项很多,但当我试图显示所有子类别时,我在控制台错误“无法读取属性‘标题’的未定义”。我不明白为什么。
在我的contentful-api服务中,我有:
getCategory(query?: object): Promise<EntryCollection<Category>> {
return this.clientApi.getEntries<Category>(
Object.assign({}, query, { content_type: 'categories', include: 2})
);}
在这之后,在我的component.ts文件中,我有:
export class Component implements OnInit {
categories: Array<Entry<Category>>;
constructor(private contentfulApiService: ContentfulApiService) { }
ngOnInit(): void {
this.contentfulApiService
.getCategory()
.then((categories) => (this.categories = categories.items));
}
}在我的component.html中:
<ul>
<li *ngFor="let category of categories">
{{ category.fields.subCategories.fields.title }}
</li>
</ul>如果我将引用从Contentful切换到single,一切都会正常工作。但是我需要很多,这样我就可以分配许多子类别并显示所有内容。
请帮帮我。
发布于 2020-08-14 05:54:09
无法读取未定义的属性“”title“”
意味着您正在尝试访问未定义的内容的title属性。看看你的代码,它只能是这一行。
{{ category.fields.subCategories.fields.title }}我假设subCategories是一个集合,因此是一个array。array没有fields属性(它是未定义的)。这就是无法访问title的原因--因为它是undefined。
如果真是这样,并且它确实是一个集合,那么您可能希望再次迭代所有subCategories以访问每个子类别的字段和标题。
https://stackoverflow.com/questions/63402891
复制相似问题