I have the following code:
setGlobalReceiverData(id) {
switch (id) {
case 'role':
for (let i = 0; i < this.roleData.length; i++) {
if (this.roleData[i]['Id'] == this.role) {
this.DataService.receiverRoleValue = this.roleData[i]['role'];
}
}
break;
case 'salutation':
for (let i = 0; i < this.salutationData.length; i++) {
if (this.salutationData[i]['Id'] == this.salutation) {
this.DataService.receiverSalutationValue = this.salutationData[i]['salutation'];
}
}
break;
case 'firstname':
this.DataService.receiverFirstnameValue = this.firstname;
break;
case 'street':
this.DataService.receiverStreetValue = this.street;
break;
case 'zip':
this.DataService.receiverZipValue = this.zip;
break;
case 'location':
this.DataService.receiverLocationValue = this.location;
break;
case 'title':
for (let i = 0; i < this.titleData.length; i++) {
if (this.titleData[i]['Id'] == this.title) {
this.DataService.receiverTitleValue = this.titleData[i]['title'];
}
}
break;
case 'lastname':
this.DataService.receiverLastnameValue = this.lastname;
break;
case 'inbox':
this.DataService.receiverInboxValue = this.inbox;
break;
case 'co':
this.DataService.receiverCoValue = this.additional_location;
break;
}
}
First of all, does the Case 'Salutation' make sense in the first place?
My main problem is however that the variable this.DataService.receiverSalutationValue is undefined, even though FirstName and LastName is defined and working properly I have another function:
public getGlobalReceiverData(): void
{
console.log('Salutation:', this.DataService.receiverSalutationValue);
console.log('FirstName:', this.DataService.receiverFirstnameValue);
console.log('Lastname', this.DataService.receiverLastnameValue);
this.salutation = this.DataService.receiverSalutationValue;
this.firstname = this.DataService.receiverFirstnameValue;
this.lastname = this.DataService.receiverLastnameValue;
this.street = this.DataService.receiverStreetValue;
this.zip = this.DataService.receiverZipValue;
this.location = this.DataService.receiverLocationValue;
this.additional_location = this.DataService.receiverCoValue;
this.inbox = this.DataService.receiverInboxValue;
}
As you can see receiver salutation is included here. However, when I start the app the salutation variable gets console logged as undefined... In my globaldata Service the global/public variables are set as they're supposed to:
public receiverRoleValue;
public receiverSalutationValue;
public receiverFirstnameValue: string;
public receiverLastnameValue: string;
public receiverStreetValue: string;
public receiverInboxValue: string;
public receiverZipValue: string;
public receiverLocationValue: string;
public receiverCoValue: string;
public receiverTitleValue;
I also have a prefill function:
public prefillSenderData() {
return this.http.get(this.get_sender_url)
.subscribe(
senderResponse => {
this.senderSalutationValue = senderResponse[0]['Salutation'];
this.senderFirstnameValue = senderResponse[0]['FirstName'];
this.senderLastnameValue = senderResponse[0]['LastName'];
this.senderPhonenumberValue = senderResponse[0]['PhoneNumber'];
this.senderPhoneValue = senderResponse[0]['Phone'];
this.senderNameValue = senderResponse[0]['Name'];
this.senderAddressValue = senderResponse[0]['Address'];
this.senderLocationValue = senderResponse[0]['Location'];
this.senderNameLeftValue = senderResponse[0]['NameLeft'];
this.senderNameRightValue = senderResponse[0]['NameRight'];
this.senderFaksimilieLeftValue = senderResponse[0]['FaksimileLeft'];
this.senderFaksimilieRightValue = senderResponse[0]['FaksimileRight'];
console.log("Sender-Object: ", senderResponse);
this.bool_senderSharedDataLoaded = true;
},
error => console.log('There was an error: ', error));
}
The salutation data array gets created and its data is mapped:
// data
public salutationData: { [salutation: string]: Object }[] = [ {salutation: "Mr.", Id: 1 }, {salutation: "Mrs.", Id: 2 } ];
public roleData: { [role: string]: Object }[] = [ {role: "Contract customer", Id: 1 }, {role: "Insurance user", Id: 2 } ];
public titleData: { [title: string]: Object }[] = [ {title: "Dr.", Id: 1 }, {title: "Prof.", Id: 2 } ];
// mapping
public salutation: Object = { text: 'salutation', value: 'Id' };
public role: Object = { text: 'role', value: 'Id' };
public title: Object = { text: 'title', value: 'Id' };