I'm currently having an issue with assigning data from an API to my Angular component file. Whenever I try to assign the data to my object variable, I receive an error stating: "cannot set property of undefined." Below is the relevant code snippet:
Class : UserDetails as
export class UserDetails {
DOB?: string;
Gender?: string;
CellPhone?: string;
}
Component.ts :
export class PerComponent implements OnInit {
public userProfileData: UserDetails;
constructor( private commonService : CommonService) {
}
ngOnInit(): void {
this.loadData();
}
loadData() {
let profileDetailParam = {ProcedureName: 'myprocname', Parameters: ['myparam']};
this.commonService.GetDataFromStoredProcedure(profileDetailParam)
.subscribe((data) => {
let parseString = require('xml2js').parseString;
let jsonData: UserDetails[];
parseString(data, function (err, result) {
if (result.NewDataSet != null)
jsonData = result.NewDataSet.SPGetDetails[0];
this.userProfileData = jsonData ;
console.log(this.userProfileData);
});
});
}
}
The jsondata variable contains the expected data in JSON format.
Upon execution, I encounter the following error: core.js:4197 ERROR TypeError: Cannot set property 'userProfileData' of undefined
Although I understand that I need to initialize the userProfileDetails variable, I have tried doing so in both the constructor and ngOnInit methods without success.