使用Angular,並嘗試將數據推送到某些數據爲空的表單。嘗試映射數據模型時,我遇到了傳入數據屬性爲空時拋出異常的問題。帶有空值的Angular.io屬性拋出未定義錯誤
這是我的輸入數據類:
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;
}
數據跨越從數據庫移動到角服務就好並得到組件作爲稱爲data
一個對象,它是Inputdata
類型的陣列。在訂閱打電話,我喜歡這個對面Inputdata分配數據給Person數據類型:
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);
}
}
}
,這裏是數據的樣本,因爲它來自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 } ]
我該如何解決這個問題,它將接受空值?
你是如何初始化「person」的? – Alex
要調試你的問題,你可以做兩件事:(1)在問題中顯示你得到的確切錯誤信息; (2)在代碼中註釋儘可能多的行以獲取觸發錯誤的最小代碼(或者用盡可能多的字段替換虛擬字符串的輸入數據)。 – ConnorsFan
我確定問題在於我忘記將某些東西導入組件以使其可以正常工作。不記得手,但我繼續前進。無論如何感謝 –