使用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
我确定问题在于我忘记将某些东西导入组件以使其可以正常工作。不记得手,但我继续前进。无论如何感谢 –