Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Property 'clientId' is used before its initialization

In my component i have declared

export class UserPatientsReportFormComponent implements OnInit {
clientId: number ;

this is my oninit function

ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe(params => {
      
      this.clientId = params.id == undefined ? null : this.commonService.encryptValue(params.id, false);
      
    });

and this is where i tried to use this clientId and face the issue.Below in patientId i tried to use this.clientId.Here i face the issue. how should i fixed it

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

patientReportForm=this.getGroup({patientId : this.clientId ,hubxCategoryId:"",notes:""})
getGroup(data:any=null)
{
  data=data || {patientId : 0,hubxCategoryId :"",notes:""}
  return new FormGroup({
    patientId:new FormControl(data.patientId),
    hubxCategoryId :new FormControl(data.hubxCategoryId),
    notes:new FormControl(data.notes)
  })
}

this.clientId value comes at other line of code but not here.
when i use in the below code this.clientId works. but no in the above code that i shared

this.patientReportForm = this.formBuilder.group({
       patientId : new FormControl(Number(this.clientId)),
      hubxCategoryId: ['',Validators.required],

this is my model.Here clientId holds the patientId value.

export class HubxModel{ 
    id: number;
    categoryId: number;
    itemTitle: string;
    itemUnit: string;
    isActive: boolean=true;
    itemValue: string;
    patientId: number;
    isDeleted: boolean;
}

here clientId value has received but when i submit patientid is null
clientid

>Solution :

this because the strict is enabled in tscconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,  🤯🤯

so you will force to set an initial value to class property

clientId: number = 0;

you can read more about this here strict and this rule strictPropertyInitialization

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading