Attempting to store form control values in an object for passing into an onSubmit method, encountered an error when assigning the form control values to the object. The error message
TS2531: Object is possibly 'null'
appears when trying to access the filters object properties within the onSubmit() method.
Here's the code snippet:
export class SearchBarComponent implements OnInit {
filtersForm! : FormGroup;
constructor(private jobService : JobsService) { }
@Output() OutputFilter = new EventEmitter<string>();
filters! : {
city: string,
level: string
}
ngOnInit(): void {
this.filtersForm = new FormGroup({
city: new FormControl('Italy, Roma'),
level: new FormControl(),
})
}
onSubmit(): void{
this.filters = {
city : this.filtersForm.get('city').value,
level : this.filtersForm.get('level').value
}
this.OutputFilter.emit(this.filters);
console.log('Submitted');
}
}
Having trouble understanding what's causing the issue. Attempted to use both ! and ? operators but haven't been able to pinpoint the problem.