使用Typescript和Angular 7初始化嵌套的json

时间:2019-04-28 15:22:18

标签: angular typescript

我正在通过发送对象来执行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”

1 个答案:

答案 0 :(得分:0)

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

   objEnvio:any = <any>{
            DocumentLines:<any>[
                 <any>{

               }
          ]
        };