I am taking data from API to my Angular component file but when I assigned the data to my object variable It gives me error of cannot set property of undefined. Here is my code :
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);
        });
      });
  }
}
jsondata contains my expected data in JSON format.
I am getting the error core.js:4197 ERROR TypeError: Cannot set property 'userProfileData' of undefined
I understand that i didn't initialise the userProfileDetails but how and where should be done.
I tried in constructor and ngOnInit but nothing works
 
     
     
    