TypeError: Cannot read property 'ShoppingCart' of undefined at Object.eval

TypeError: Cannot read property 'ShoppingCart' of undefined at Object.eval

.html

  <div *ngFor="let p of (products$ | async)?.result.categories.ShoppingCart;">
        {{p.price}}
   </div>

.ts

this.products$ = this.bikeShopService.get();

邮递员

{
    "result": [
        {
            "categories": [
                {
                    "ShoppingCart": [
                        {
                            "id": 1,
                            "name": "Golf Clubs",
                            "price": "125",

}]}]}

ERROR TypeError: Cannot read property 'ShoppingCart' of undefined at Object.eval [as updateDirectives] (BikeShopListPage.html:22) at Object.debugUpdateDirectives [as updateDirectives] (core.js:23813) at checkAndUpdateView (core.js:23209) at callViewAction (core.js:23450) at execComponentViewsAction (core.js:23392) at checkAndUpdateView (core.js:23215) at callViewAction (core.js:23450) at execEmbeddedViewsAction (core.js:23413) at checkAndUpdateView (core.js:23210) at callViewAction (core.js:23450)

问:你能告诉我如何访问以上数据吗?

应该是.result[0]?.categories[0]?.ShoppingCart

resultcategories 是数组而不是对象

您的项目是数组,您可能需要考虑取出数组部分 [],或者如果需要 arrays/lists,请提供数组 UID 中的对象

ngFor 可用于 HTML 或函数中的 .map()