简体   繁体   English

找不到属性angular2和打字稿

[英]Property not found angular2 and typescript

I have an api that returns me a json , and I cast the response to my class this works fine. 我有一个向我返回json的api,并且将响应投射到了我的课程上,效果很好。 But the json doesnt contains some varibales in the class and I expect those variables to be initialed with null if its not being set. 但是json在类中不包含一些变量,如果没有设置,我希望这些变量以null开头。 But they dont appear at all(they disappear) and i get an exception that they are not there whiles they should be there. 但是它们根本不出现(它们消失了),而我得到的例外是它们不在那儿,而应该在那儿。 Pls an explanation on why this is happening would be very satisfying. 请解释为什么会发生这种情况非常令人满意。

export class CustomPurchaseOrderProduct {
  constructor(
    public productId: number,
    public productName: string,
    public warehouseName: string,
    public binLocationCode: string,
    public amountInStock: number,
    public sellingPrice: number,
    public productTaxRate: number,
    public calculatedTax: number,
    public unitPrice: number,
    public discount: number
  ) {}
}

  getCustomProductOrders() {
const url = `${this.localUrl}purchaseOrderProducts`;
return this.http.get(url)
  .map(response => <CustomPurchaseOrderProduct[]>response.json());

} }

  getAllPurchaseOrderProducts() {
this.purchaseOrderService.getCustomProductOrders()
    .subscribe(
      response => this.customPurchaseOrderProducts = response
    );

} }

console.log('This is form Object', this.customPurchaseOrderProducts[index].calculatedTax);

JSON Object from array 数组中的JSON对象

{
"productId": 8,
"productName": "Akomka ",
"warehouseName": null,
"binLocationCode": null,
"amountInStock": 0,
"sellingPrice": 0,
"productTaxRate": 10,
"unitPrice": 0

} }

So I am expecting my CalculatedTax ,UnitPrice and Discount to be null but they are not there when I console log the object 所以我期望我的CalculatedTax,UnitPrice和Discount为null,但是当我控制台记录对象时它们不存在

The calculatedTax and other variables are just not in the object when I console log them. 当我控制台记录它们时,calculatedTax和其他变量只是不在对象中。

By doing this: 通过做这个:

.map(response => <CustomPurchaseOrderProduct[]>response.json());

you are just telling compiler that you know the shape of the data and that you are sure it matches your class. 您只是告诉编译器您知道数据的形状,并且确定它与您的类匹配。 If you console log the response you can see that it's a plain POJO and will console log Object {...} , so it's really not an instance of your class. 如果您控制台记录响应,您会发现它是一个普通的POJO,并将控制台记录Object {...} ,因此它实际上不是您的类的实例。

If you truly want it to be an instance of your class and have properties that potentially are not there, you can as mentioned in comments have them as optional properties. 如果您确实希望它成为类的实例并且具有可能不存在的属性,则可以如注释中所述将它们作为可选属性。 And if the property does not exist, then assign the null value. 并且如果该属性不存在,则分配空值。 So your shortened class: 因此,您的班级缩短了:

export class CustomPurchaseOrderProduct {
  private productId: number;
  private productName: string;
  private calculatedTax?: number;

  constructor(
    productId: number,
    productName: string,
    calculatedTax?: number

) {
    this.productId = productId;
    this.productName = productName;
    this.calculatedTax = calculatedTax || null
  }
}

So above we are marking calculatedTax as optional, and if it's not present in response, we assign null to the property. 因此,在上面,我们将calculatedTax标记为可选,如果响应中不存在它,则将null分配给该属性。 Please notice that you need to mark the optional properties as last in the constructor. 请注意,您需要将可选属性标记为构造函数中的最后一个。

Then in the http-request you need to create the incoming data as an instances of your class, so you would need to do: 然后,在http请求中,您需要创建传入数据作为类的实例,因此您需要执行以下操作:

getCustomProductOrders() {
  const url = `${this.localUrl}purchaseOrderProducts`;
  return this.http.get(url)
   .map(response => <CustomPurchaseOrderProduct[]>response.json()
     .map(data => 
      new CustomPurchaseOrderProduct(data.productId, data.productName, data.calculatedTax)
     )
   )
}

Using classes vs Interfaces is an debatable issue, some prefer interfaces, some classes. 使用类vs接口是一个有争议的问题,有些更喜欢接口,有些类。 So you could also use interfaces instead, but that alone will not solve the issue though. 因此,您也可以改用接口,但是仅靠它并不能解决问题。 Solving this could be done also when you are getting response, iterate the data and check if property exists in object, if not, then assign it with null . 当您获得响应时,也可以解决此问题,对数据进行迭代并检查对象中是否存在属性(如果不存在),然后将其分配为null But above is solution is with using class :) 但是上面的解决方案是使用class :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM