Dealing with Angular and facing an issue while trying to populate a form with null data. The problem arises when mapping the data model as exceptions are thrown if the incoming data property is null.
Below is my input data class:
export class Inputdata {
sweepId: string;
IndivId: string;
FirstName: string;
PrefName: string;
LastName: string;
Suffix: string;
Language: string;
Addr1: string;
Addr2: string;
Addr3: string;
City: string;
State: string;
Zip: string;
CountryCode: string;
Country: string;
PrimaryPhoneType: string;
PrimaryPhone: string;
PrimaryEmailType: string;
PrimaryEmail: string;
Positioncode: string;
TrackingNumber: string;
PositionTitle: string;
AreaServed: string;
DistrictServed: string;
TrackingID: string;
}
The data successfully moves from the database to the Angular service and reaches the component as an object named data
, which is an array of type Inputdata
. Within the subscribe call, I am transferring data from Inputdata to the Person data type like this:
this.people.getPersonById(this.Id).subscribe(data=>{
this.inputs = data;
console.log(this.inputs[0]);
this.person.email.priEmailType = this.inputs[0].PrimaryEmailType || 'Not Provided';
this.person.email.emailAddress = this.inputs[0].PrimaryEmail;
this.person.phone.priPhoneType = this.inputs[0].PrimaryPhoneType;
this.person.phone.Phone = this.inputs[0].PrimaryPhone;
this.person.name.firstName = this.inputs[0].FirstName;
this.person.name.prefName = this.inputs[0].PrefName;
this.person.name.lastName = this.inputs[0].LastName;
this.person.name.suffix = this.inputs[0].Suffix;
this.person.address.addr1 = this.inputs[0].Addr1;
this.person.address.addr2 = this.inputs[0].Addr2;
this.person.address.addr3 = this.inputs[0].Addr3;
this.person.address.city = this.inputs[0].City;
this.person.address.state = this.inputs[0].State;
this.person.address.zip = this.inputs[0].Zip;
this.person.address.country = this.inputs[0].Country;
this.person.address.countryCode = this.inputs[0].CountryCode;
if(this.inputs.length >1){
for(var i=0;i<this.inputs.length;i++){
this.position.positionCode = this.inputs[i].Positioncode;
this.position.positionTitle = this.inputs[i].PositionTitle;
this.person.positions.push(this.position);
}
}
}
Here is a snippet of the data retrieved from the DB:
[ { SweepID: 1,
IndivId: 404873,
FirstName: 'Paul',
PrefName: null,
LastName: 'Person',
Suffix: null,
Language: 'EN',
Addr1: '13120 Skidaway Place',
Addr2: null,
Addr3: null,
City: 'Tarry Town',
State: 'LOUISIANA ',
Zip: '70737',
CountryCode: 'US',
Country: 'United States',
PrimaryPhoneType: 'Home',
PrimaryPhone: '(225)572-2268',
PrimaryEmailType: null,
PrimaryEmail: null,
PositionCode: 'GM',
TrackingNumber: '000131960',
PositionTitle: 'General Manager',
AreaServed: '027',
DistrictServed: null,
TrackingID: null },
{ SweepID: 82257,
IndivId: 404873,
FirstName: 'Paul',
PrefName: null,
LastName: 'Person',
Suffix: null,
Language: 'EN',
Addr1: '13120 Skidaway Place',
Addr2: null,
Addr3: null,
City: 'Tarry Town',
State: 'LOUISIANA',
Zip: '11111',
CountryCode: 'US',
Country: 'United States',
PrimaryPhoneType: 'Home',
PrimaryPhone: '(555)555-5555',
PrimaryEmailType: null,
PrimaryEmail: null,
PositionCode: 'PRCONT',
TrackingNumber: '000131960',
PositionTitle: 'Primary Contact',
AreaServed: null,
DistrictServed: null,
TrackingID: null } ]
How can I handle this issue and make it accept Null Values?