Edit: From what i've understood reading over and over, the issue is that i can't access the properties from currentPerson. And this is where all errors come from. But i compared to other component file and they were almost the same.
First of all, i've checked the links when typing my question title. While the error code is the same, unfortunately i wasn't able to find an proper answer.
I'm totally new to angular\typescript, and i've been following a tutorial i've found on this link: https://www.techiediaries.com/angular-11-crud-rest-api-tutorial/
In order to understand how things works, i've changed product to person (and added more details like surname, address, etc). Everything was going fine, until i've got the part of updating a person details.
When i reached that point (editing person-details-components.ts), when i try to change the method 'updatePerson(): void {}' i'm having the error:
Object is possibly null . ts(2531).
I've tried turning off strictNullCheck on tsconfig.app.json , nothing changed. I've also tried to add the ? operator on the line showing the error, didn't worked also (the error changes, property doesn't exist on type 'never').
Since i'm new to angular, i'm only following the tutorial and trying to undersatnd where the error comes from. I noticed i can access currentperson, but none of its properties (name, address, etc).
I've also compared it to other component.ts files, and there i was able to access the properties i mentioned. What am i doing wrong? And sorry for the bad english.
Thanks.
Heres my 'person-details-components.ts' file:
import { Component, OnInit } from '@angular/core';
import { PersonService } from 'src/app/services/person.service';
import { ActivatedRoute, Router } from '@angular/router';
@Component({
selector: 'app-person-details',
templateUrl: './person-details.component.html',
styleUrls: ['./person-details.component.css']
})
export class PersonDetailsComponent implements OnInit {
currentperson = null;
message = '';
constructor(
private personService: PersonService,
private route: ActivatedRoute,
private router: Router) { }
ngOnInit(): void {
this.message = '';
this.getPerson(this.route.snapshot.paramMap.get('id'));
}
getPerson(id: any): void {
this.personService.read(id)
.subscribe(
person => {
this.currentperson = person;
console.log(person);
},
error => {
console.log(error);
});
}
//All errors start here. I can't access this.currentperson.name and so on
setAvailableStatus(status: any): void {
const data = {
name: this.currentperson.name,
description: this.currentperson.description,
available: status
};
this.personService.update(this.currentperson.id, data)
.subscribe(
response => {
this.currentProduct.available = status;
console.log(response);
},
error => {
console.log(error);
});
}
updateProduct(): void {
this.productService.update(this.currentProduct.id, this.currentProduct)
.subscribe(
response => {
console.log(response);
this.message = 'The product was updated!';
},
error => {
console.log(error);
});
}
deleteProduct(): void {
this.productService.delete(this.currentProduct.id)
.subscribe(
response => {
console.log(response);
this.router.navigate(['/products']);
},
error => {
console.log(error);
});
}
}