Mit Angular arbeiten und versuchen, Daten in ein Formular zu übertragen, in dem einige der Daten null sind. Beim Versuch, das Datenmodell zuzuordnen, treten Probleme auf, wenn Ausnahmen ausgelöst werden, wenn die Eigenschaft für eingehende Daten null ist.Angular.io-Eigenschaft mit Nullwert undefiniertem Fehler
Hier ist meine Eingangsdatum Klasse:
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;
}
Die Daten bewegen sich gegenüber von der Datenbank in den Winkel Service ganz gut und werden auf die Komponente als ein Objekt namens data
die Inputdata
ein Array vom Typ ist. In der Zeichnung bezeichnen, ich Daten über von Eingangsdaten an der Person Datentyp wie diese Zuordnung:
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);
}
}
}
und hier ist eine Probe der Daten, wie sie von der DB kommt.
[ { 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 } ]
Wie kann ich das umgehen, wo es Nullwerte akzeptiert?
Wie hast du 'person' initialisiert? – Alex
Um Ihr Problem zu debuggen, können Sie 2 Dinge tun: (1) Zeigen Sie in der Frage die genaue Fehlermeldung an, die Sie erhalten; (2) Kommentieren Sie so viele Zeilen wie möglich in Ihrem Code, um den minimalen Code zu erhalten, der den Fehler auslöst (oder ersetzen Sie die Eingabedaten durch Dummy-Strings für so viele Felder wie möglich). – ConnorsFan
Ich stellte fest, das Problem war, dass ich vergessen habe, etwas in die Komponente zu importieren, damit es funktioniert. Erinnere dich nicht an die Hand, aber ich ging weiter. danke trotzdem –