2017-07-26 109 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

你能否用你得到的實際錯誤信息更新這個問題? –

相關問題