typescript-json-serializer: Cannot read property 'name' of undefined

Hey dude! I’m using your nice lib. but today i got a error where just ocurred when i run ng buil – prod

So this bug is: if in this class of object not have some name of JsonProperty that has in the response i got this error. Screenshot 2021-04-15 174013

this is my class object

@Serializable()
export class Vehicle {

  constructor(init?: Partial<Vehicle>) {
    Object.assign(this, init);
  }

  @JsonProperty('CLV_CD_CLIENTE_VEICULO')
  idClientVehicle?: number = 0;

  @JsonProperty('CLV_DS_VEICULO')
  name?: string = undefined;

  @JsonProperty('CLV_DS_PLACA')
  board?: string = undefined;

  @JsonProperty('CLV_DS_CARTAO')
  card?: string = undefined;

  @JsonProperty('CLV_VL_LIMITE_REAIS')
  limitReais?: number = undefined;

  @JsonProperty('CLV_VL_LIMITE_LITROS')
  limitLiter?: number = undefined;

  @JsonProperty('CLV_FL_ATIVO')
  state?: boolean | STATUS = STATUS.ACTIVE;

  @JsonProperty('CLV_VL_LIMITE_DISPONIVEL_REAIS')
  limitReaisAvailable?: number = undefined;

  @JsonProperty('CLV_VL_LIMITE_DISPONIVEL_LITROS')
  limitLiterAvailable?: number = undefined;

  @JsonProperty('CCC_DS_DESCRICAO')
  description?: string = undefined;

  @JsonProperty({ name: 'CLV_DT_VALIDADE_TACOGRAFO', beforeDeserialize: stringToDate, beforeSerialize: dateToString })
  dateTacografo?: Date = undefined;

}

export function deserializeVehicle(value) {
  return deserializer({ OBJ: value }, VehicleArray).array;
}
@Serializable()
class VehicleArray {
  @JsonProperty({ name: 'OBJ', type: Vehicle })
  array: Vehicle[];
}

This is the response

[
    {
        "CLV_CD_CLIENTE_VEICULO": 316005,
        "CLV_DS_VEICULO": "ASDASDASD",
        "CLV_DS_PLACA": "ASD-3434",
        "CLV_VL_LIMITE_REAIS": 0,
        "CLV_VL_LIMITE_DISPONIVEL_REAIS": 0,
        "CLV_VL_LIMITE_LITROS": 0,
        "CLV_VL_LIMITE_DISPONIVEL_LITROS": 0,
        "CLV_DS_CARTAO": null,
        "CLV_FL_ATIVO": "S",
        "CCC_CD_CLIENTE_CENTRO_CUSTO": 372,
        "CCC_DS_DESCRICAO": "ADMINISTRAÇÃO",
        "CLV_FL_LIMITE_REAIS": "S",
        "CLV_FL_LIMITE_LITROS": "N",
        "CLV_DT_VALIDADE_TACOGRAFO": "26/03/2021"
    },
    {
        "CLV_CD_CLIENTE_VEICULO": 258022,
        "CLV_DS_VEICULO": "CARRO",
        "CLV_DS_PLACA": "KKK-7777",
        "CLV_VL_LIMITE_REAIS": 333.2,
        "CLV_VL_LIMITE_DISPONIVEL_REAIS": 333.2,
        "CLV_VL_LIMITE_LITROS": 333.33,
        "CLV_VL_LIMITE_DISPONIVEL_LITROS": 333.33,
        "CLV_DS_CARTAO": "S",
        "CLV_FL_ATIVO": "S",
        "CCC_CD_CLIENTE_CENTRO_CUSTO": 372,
        "CCC_DS_DESCRICAO": "ADMINISTRAÇÃO",
        "CLV_FL_LIMITE_REAIS": "S",
        "CLV_FL_LIMITE_LITROS": "S",
        "CLV_DT_VALIDADE_TACOGRAFO": "31/03/2021"
    },
    {
        "CLV_CD_CLIENTE_VEICULO": 371333,
        "CLV_DS_VEICULO": "DADASDSA",
        "CLV_DS_PLACA": "ASDASDA",
        "CLV_VL_LIMITE_REAIS": 0,
        "CLV_VL_LIMITE_DISPONIVEL_REAIS": 0,
        "CLV_VL_LIMITE_LITROS": 0,
        "CLV_VL_LIMITE_DISPONIVEL_LITROS": 0,
        "CLV_DS_CARTAO": null,
        "CLV_FL_ATIVO": "S",
        "CCC_CD_CLIENTE_CENTRO_CUSTO": null,
        "CCC_DS_DESCRICAO": null,
        "CLV_FL_LIMITE_REAIS": "S",
        "CLV_FL_LIMITE_LITROS": "S",
        "CLV_DT_VALIDADE_TACOGRAFO": null
    }
]

So i don’t have in my class this names: CLV_FL_LIMITE_REAIS CLV_FL_LIMITE_LITROS CCC_CD_CLIENTE_CENTRO_CUSTO

Are there some thing to resolve this in your lib?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 23 (12 by maintainers)

Most upvoted comments

Solved it by adding "emitDecoratorMetadata": true to the tsconfig.json file.

That’s fair enough. Let me do that ASAP.

My lib is not created specifically for React so I will not add this config directly but I can create a section in the ReadMe to explain how to fix the issue.

Could you provide here the code you added to make it work please 😊.

Ok ok, sorry 😕