【问题标题】:Initialize nested json using Typescript and Angular 7使用 Typescript 和 Angular 7 初始化嵌套的 json
【发布时间】:2019-09-17 09:23:36
【问题描述】:

我正在通过发送一个对象来做一个 POST 请求,但我得到一个错误错误

TypeError: 无法设置未定义的属性“ItemCode”

我正在使用带有打字稿的 Angular 7

这是我的初始化 Json

    objEnvio:any = <any> {
        DocumentLines:<any>[]
    };

这是我的代码:

    Cadastrar() {
        var today = new Date();
        var self = this;
        this.objEnvio.CardCode = something;
        this.objEnvio.DocDueDate = something;
            this.objEnvio.BPL_IDAssignedToInvoice = something;
            $('.table-servico .mat-row').each(function(index, element){
                     self.objEnvio.DocumentLines[index].ItemCode =something;   // this line is throwing the error

预期结果:

   {
       "CardCode": "C000009",
       "DocDueDate": "2019-04-25",
       "BPL_IDAssignedToInvoice": 1,
       "DocumentLines": [
          {
          "ItemCode": "IT0001",
          "UnitPrice": 207,
          "Quantity": 4,
          "TaxCode": "1101-001",
          },
          {
          "ItemCode": "SV0003",
          "UnitPrice": 110,
          "Quantity": 1,
          "TaxCode": "1933-001",
          },
         ]
     }

当前实际结果:

TypeError:无法设置未定义的属性“ItemCode”

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    现在可以了,只需添加另一个

       objEnvio:any = <any>{
                DocumentLines:<any>[
                     <any>{
    
                   }
              ]
            };
    

    【讨论】:

      猜你喜欢
      • 2019-03-28
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多