I have a problem in my angular app that says ERROR TypeError: Cannot read property 'name' of undefined but the projects.name is displaying. How can i get rid of the error in the console.log. The data contains an object.
ngOnInit() {
this.route.params
.subscribe((params: Params) => {
this.id = +params['id'];
this.projectsService = this.injector.get(ProjectsService);
this.projectsService.getProject(this.id)
.subscribe(
(data:any) => {
this.projects = data;
console.log(data);
},
error => {
alert("ERROR");
})
});
}
Seems like you directly used projects.name
and projects
is being retrieved by ajax. This code fails because it tries to projects.name
binding on initial change detection run.
In such cases Use Safe Navigation / Elvis
operator
{{projects?.name}}