Let's take a look at my main class A
export class A {
public sourceReference: string;
public charge: Charge;
}
Here is how my Charge class is structured:
export class Charge {
public chargeType: string;
public chargeAccountNumber: string;
public chargeCurrency: string;
}
Now, in the index file, I intend to utilize class A and populate the payload like so:
import { A } from "./a";
public static create(payload) {
let myData = new A();
myData.sourceReference = payload.sourceRef;
myData.charge.chargeType = payload.type;
myData.charge.chargeAccountNumber = payload.accountNumber;
myData.charge.chargeCurrency = payload.currency
return JSON.stringify(myData)
}
The incoming payload data appears as follows:
payload = { sourceRef: "1234", type: '615-01', accountNumber: '123456', currency: 'USD'}
I can successfully access myData.sourceReference
However,
The issue arises when trying to access myData.charge
, which returns as undefined. This results in undefined values for myData.charge.chargeType
,
myData.charge.chargeAccountNumber
& myData.charge.chargeCurrency
.
Error Message: TypeError: Cannot set property 'chargeType' of undefined