类型错误:无法读取 Object.eval 上未定义的属性"购物车"



.html

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

.ts

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

Postman

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

错误typeerror:无法阅读未定义的属性'shoppingcart' 在object.eval [AS UpdateDirectives](bikeshoplistpage.html:22) 在object.debugupdatedirectives [as Updatedirectives](core.js:23813) 在checkandupdateview(core.js:23209) 在CallViewAction(Core.JS:23450) 在ExecComponentViewSAction(core.js:23392) 在ChectAndUpDateView(Core.JS:23215) 在CallViewAction(Core.JS:23450) 在ExecembedDedViewSaction(core.js:23413) 在checkandupdateview(core.js:23210) 在CallViewAction(Core.JS:23450)

Q:您能告诉我如何访问上述数据吗?

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

结果类别是数组而不是对象

您的项目是数组,您可能需要考虑取出数组零件[],或者如果需要数组/列表,则将对象添加到您的数组UID的

ngfor可以在函数中的html或.map()中使用

最新更新