2017-07-26 108 views
0

使用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 } ] 

我该如何解决这个问题,它将接受空值?

+0

你是如何初始化“person”的? – Alex

+1

要调试你的问题,你可以做两件事:(1)在问题中显示你得到的确切错误信息; (2)在代码中注释尽可能多的行以获取触发错误的最小代码(或者用尽可能多的字段替换虚拟字符串的输入数据)。 – ConnorsFan

+0

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

回答

0

你到这里:

this.person.email.priEmailType = this.inputs[0].PrimaryEmailType || 'Not Provided';

只是做相同的其他领域以及:

this.person.name.lastName = this.inputs[0].LastName || '';

如果名字为空/未定义,它会默认以空字符串

+0

我已经尝试过,结果不好,但谢谢! –

+0

事情是我做了'||'''事,但它仍然给出了同样的错误... –

1

我正在这里出门......

我怀疑你的实际问题可能是与person铺设,这是抛出你的错误。具有null值的响应不应该抛出这样的错误。

所以,当你还没有初始化它正确它试图读取不存在的,例如财产路径:

person.email.priEmailType 

其中person可能是一个空的对象,但emailundefined,所以当它试图阅读priEmailType,它不能从emailundefined

速战速决是初始化对象:

person = {email:{}, phone:{}, name:{}, address:{}} 

因此,例如,现在我们可以尝试阅读priEmailType因为email不再undefined

您可能需要做一些辅助方法,或至少将值映射到您的person,特别是如果您想在别处使用它的话。

+0

只想睡觉前留下一个答案,即使我还没有得到答案对我的评论。我怀疑我有正确的诊断tho:D如果没有,我会删除... – Alex

+0

你能否用你得到的实际错误信息更新这个问题? –

相关问题