2017-07-26 13 views
0

Angularを使用し、データの一部がnullのフォームにデータをプッシュしようとしました。データモデルをマップしようとすると、着信データプロパティがnullの場合に例外がスローされる問題があります。角度サービスにデータベースから間のデータ移動がうまくnull値を持つ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; 
} 

及びタイプInputdataの配列であるdataと呼ばれるオブジェクト、コンポーネントになる:ここ

は私の入力データ・クラスです。呼び出すサブスクライブでは、私はこのような人のデータ型に入力データから全体のデータを代入しています:

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

Null値を受け入れる場所をどうすれば解決できますか?

+0

「人」をどのように初期化しましたか? – Alex

+1

問題をデバッグするには、2つのことができます。(1)質問に正確なエラーメッセージを表示する。 (2)可能な限り多くの行をコメントアウトして、エラーを引き起こす最小限のコードを取得します(または、入力データを可能な限り多くのフィールドに対してダミー文字列で置き換えます)。 – ConnorsFan

+0

私は問題を、コンポーネントを動作させるためにコンポーネントにインポートするのを忘れていたと判断しました。手を覚えていないが、私は動いた。とにかくありがとう –

答えて

0

それをここまでにしている:

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

だけで、他のフィールドに同じ操作を行います。

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

姓は/それがデフォルトになりますnullの定義されていない場合空の文字列に

+0

私は悪い結果でそれを試みたが、ありがとう! –

+0

事は私が ';| ''をしたことですが、それでもやはり同じエラーが出ました... –

1

私はここの肢に出ています...

あなたの実際の問題はおそらくpersonであると思われます。それはあなたのエラーを投げているものです。 nullの値を持つレスポンスでは、このようなエラーは発生しません。

ですから、例えば、存在しないプロパティパスを読み込むしようとしているそれを正しく初期化されていないとき:personは空のオブジェクトかもしれませんが、emailundefinedあるので、それをしようとしているとき

person.email.priEmailType 

priEmailTypeを読むには、emailundefinedであるため、できません。例えば、今、私たちがしようとemail以来priEmailTypeはもはやundefined

あなたは、いくつかのヘルパーメソッドを作成したい場合や、ある読むことができませんので

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

クイックフィックスは、などのオブジェクトを初期化することです少なくとも値をpersonにマップするものがあります。特に、別の場所で使用したい場合は特にそうです。

+0

私はまだ答えを得ていませんでした。私のコメントに。私は正しい診断のthoを持っていると思う:Dそうでなければ、私は削除する... – Alex

+0

あなたは実際のエラーメッセージで質問を更新できますか? –

関連する問題