使用Angular,并尝试将数据推送到某些数据为null的表单。在尝试映射数据模型时,我遇到了在传入数据属性为null时抛出异常的问题。
这是我的输入数据类:
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);
}
}
}
以下是来自数据库的数据样本。
[ { 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 } ]
我如何解决这个问题,接受Null值呢?
答案 0 :(得分:1)
我出去了......
我怀疑你的实际问题可能是person
,这就是你的错误。具有null
值的响应不应该抛出这样的错误。
因此,当您没有正确初始化它时,它会尝试读取不存在的属性路径,例如:
person.email.priEmailType
其中person
可能是一个空对象,但email
是undefined
,所以当它尝试阅读priEmailType
时,它就不能因为email
是undefined
。
快速解决方法是将对象初始化为:
person = {email:{}, phone:{}, name:{}, address:{}}
例如,现在我们可以尝试阅读priEmailType
,因为email
不再是undefined
您可能想要制作一些辅助方法或其他内容,至少将值映射到person
,特别是如果您想在其他地方使用它。
答案 1 :(得分:0)
你在这里:
this.person.email.priEmailType = this.inputs[0].PrimaryEmailType || 'Not Provided';
也为其他领域做同样的事情:
this.person.name.lastName = this.inputs[0].LastName || '';
如果LastName为null / undefined,则默认为空字符串