【问题标题】:TypeError: Cannot read property 'ShoppingCart' of undefined at Object.evalTypeError:无法读取 Object.eval 处未定义的属性“ShoppingCart”
【发布时间】:2019-07-29 16:44:44
【问题描述】:

.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",

}]}]}

错误类型错误:无法读取未定义的属性“购物车” 在 Object.eval [作为 updateDirectives] (BikeShopListPage.html:22) 在 Object.debugUpdateDirectives [as updateDirectives] (core.js:23813) 在 checkAndUpdateView (core.js:23209) 在 callViewAction (core.js:23450) 在 execComponentViewsAction (core.js:23392) 在 checkAndUpdateView (core.js:23215) 在 callViewAction (core.js:23450) 在 execEmbeddedViewsAction (core.js:23413) 在 checkAndUpdateView (core.js:23210) 在 callViewAction (core.js:23450)

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

【问题讨论】:

    标签: angular typescript rxjs angular7 ionic4


    【解决方案1】:

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

    resultcategories 是数组而不是对象

    【讨论】:

      【解决方案2】:

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

      ngFor 可以在 HTML 中使用,也可以在函数中使用 .map()

      【讨论】:

        猜你喜欢
        • 2020-07-15
        • 1970-01-01
        • 1970-01-01
        • 2017-09-16
        • 1970-01-01
        • 2021-03-03
        • 2020-06-09
        • 1970-01-01
        • 2020-11-29
        相关资源
        最近更新 更多