Angular2-TypeError:无法读取(Typescript)中未定义的属性'Id'

尼古拉斯

我收到以下错误:

angular2.dev.js:23925 EXCEPTION: TypeError: Cannot read property 'Id' of null in [


{{ product.Id }}
 in ProductEditComponent@0:68]

抛出:

//Product-edit.component.ts:

import {Component } from 'angular2/core';
import { IProduct } from './product'
import { ProductService } from './product.service'
import { RouteParams } from 'angular2/router';
@Component({
  template:`<div class="wrapper wrapper-content animated fadeInRight ecommerce"> 
              {{ product.Id }}
            </div>`, 
})
export class ProductEditComponent{
    product: IProduct = null;
    errorMessage: string;
    constructor(private _routeParams: RouteParams, private _productService: ProductService){

    }

    ngOnInit(): void {
        this._productService.getProduct(this._routeParams.get('id'))
            .subscribe(
                product => this.product = product,
                error => this.errorMessage = <any>error);


    }

}

产品服务:

getProduct(id: string): Observable<IProduct> {
    return this._http.get(this._baseUrl + this._getProductUrl + '/' + id)
        .map((response: Response) => <IProduct>response.json())
        .do(data => console.log("All: " + JSON.stringify(data)))
        .catch(this.handleError);
}

来自服务器的响应:

{"Id":"34d4efcy6","ExternalId":null,"UserId":"testing","ProductProvider":null,"Title":"Flaska vin","Desc":null,"MinDeliveryTime":null,"MaxDeliveryTime":null,"FreightCost":null,"Brand":null}

我搞砸了吗?

画摩尔

在您的组件中,您将初始化product为null,然后product.Id在模板中进行引用当Angular异步调用返回之前尝试初始绘制模板时发生错误-此时product仍为null,因此错误:Cannot read property 'Id' of null

最直接的解决方案是使用Elvis运算符,Angular在这种情况下提供了运算符您可以通过在模板中替换{{ product.Id }}{{ product?.Id }}使用它

就是说,您可能会用这种方法遇到变更检测问题,并且总的来说,使用以下方法会更好:

export class ProductEditComponent{
  product: Observable<IProduct>; //product is an Observable
  errorMessage: string;
  constructor(private _routeParams: RouteParams, private _productService: ProductService){
     //product is always defined because you instantiate it in the ctor
     this._productService.getProduct(this._routeParams.get('id'));
  }

然后,您{{(product | async).Id }}{{product.Id}}在模板中使用,以利用AsyncPipeangular来根据需要来订阅和更新UI的繁琐工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Angular Material TypeError:无法读取未定义的属性“ id”

来自分类Dev

错误TypeError:无法在tipo-struttura.component.ts(angular + spring + sql)读取未定义的属性'id'

来自分类Dev

类型错误:无法读取编辑视图模板上未定义的属性“Id” - Angular 2

来自分类Dev

(Angular2 和 Typescript)Angular 的 DatePicker 无法读取未定义的属性“切换”

来自分类Dev

NgZone / Angular2 / Ionic2 TypeError:无法读取未定义的属性“运行”

来自分类Dev

UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ id”

来自分类Dev

Gatsby TypeError-无法读取未定义的属性“ id”

来自分类Dev

MERN-TypeError:无法读取未定义的属性“ id”

来自分类Dev

UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ id”

来自分类Dev

JWT-TypeError:无法读取未定义的属性“id”

来自分类Dev

Webpacked Angular2应用程序TypeError:无法读取未定义的属性“ getOptional”

来自分类Dev

Webpacked Angular2应用程序TypeError:无法读取未定义的属性“ getOptional”

来自分类Dev

错误类型错误:无法读取未定义的属性“ID”(Angular 8)

来自分类Dev

Angular TypeError:无法读取未定义的属性“ then”

来自分类Dev

TypeError:无法读取未定义<Angular 8>的属性'map'

来自分类Dev

Angular:无法读取未定义TypeError的属性“ routeConfig”

来自分类Dev

Angular 9 TypeError:无法读取未定义的属性“ subscribe”

来自分类Dev

Angular / Karma TypeError:无法读取未定义的属性“ offsetTop”

来自分类Dev

Angular TypeError无法读取未定义的属性“ then”

来自分类Dev

NativeScript/Angular - TypeError:无法读取未定义的属性“符号”

来自分类Dev

Angular - TypeError:无法读取未定义的属性“推送”

来自分类Dev

Angular - TypeError:无法读取未定义的属性“订阅”

来自分类Dev

Angular 5:TypeError:无法读取未定义的属性“密码”

来自分类Dev

Angular 6:TypeError:无法读取未定义的属性“值”

来自分类Dev

无法读取未定义的属性“ id”

来自分类Dev

角度2:TypeError:无法读取未定义的属性'isRouteActive'

来自分类Dev

TypeError:无法读取未定义的属性“ body2”

来自分类Dev

无法读取未定义的Angular / TypeScript属性'forEach'

来自分类Dev

无法读取未定义的 Angular/Typescript 的属性“推送”

Related 相关文章

  1. 1

    Angular Material TypeError:无法读取未定义的属性“ id”

  2. 2

    错误TypeError:无法在tipo-struttura.component.ts(angular + spring + sql)读取未定义的属性'id'

  3. 3

    类型错误:无法读取编辑视图模板上未定义的属性“Id” - Angular 2

  4. 4

    (Angular2 和 Typescript)Angular 的 DatePicker 无法读取未定义的属性“切换”

  5. 5

    NgZone / Angular2 / Ionic2 TypeError:无法读取未定义的属性“运行”

  6. 6

    UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ id”

  7. 7

    Gatsby TypeError-无法读取未定义的属性“ id”

  8. 8

    MERN-TypeError:无法读取未定义的属性“ id”

  9. 9

    UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“ id”

  10. 10

    JWT-TypeError:无法读取未定义的属性“id”

  11. 11

    Webpacked Angular2应用程序TypeError:无法读取未定义的属性“ getOptional”

  12. 12

    Webpacked Angular2应用程序TypeError:无法读取未定义的属性“ getOptional”

  13. 13

    错误类型错误:无法读取未定义的属性“ID”(Angular 8)

  14. 14

    Angular TypeError:无法读取未定义的属性“ then”

  15. 15

    TypeError:无法读取未定义<Angular 8>的属性'map'

  16. 16

    Angular:无法读取未定义TypeError的属性“ routeConfig”

  17. 17

    Angular 9 TypeError:无法读取未定义的属性“ subscribe”

  18. 18

    Angular / Karma TypeError:无法读取未定义的属性“ offsetTop”

  19. 19

    Angular TypeError无法读取未定义的属性“ then”

  20. 20

    NativeScript/Angular - TypeError:无法读取未定义的属性“符号”

  21. 21

    Angular - TypeError:无法读取未定义的属性“推送”

  22. 22

    Angular - TypeError:无法读取未定义的属性“订阅”

  23. 23

    Angular 5:TypeError:无法读取未定义的属性“密码”

  24. 24

    Angular 6:TypeError:无法读取未定义的属性“值”

  25. 25

    无法读取未定义的属性“ id”

  26. 26

    角度2:TypeError:无法读取未定义的属性'isRouteActive'

  27. 27

    TypeError:无法读取未定义的属性“ body2”

  28. 28

    无法读取未定义的Angular / TypeScript属性'forEach'

  29. 29

    无法读取未定义的 Angular/Typescript 的属性“推送”

热门标签

归档