Chrome error on page loading “ERROR TypeError: Cannot read property 'name' of undefined”

Chrome error on page loading “ERROR TypeError: Cannot read property 'name' of undefined”



This is my employee.component.html


<div class="row">
<div class="input-field col s12">
<input type="text" name="name" #name="ngModel" [(ngModel)]="employeeService?.selectedEmployee.name" placeholder="Enter full name"
required>
<label>Name :
<label class="red-text">*</label>
</label>
</div>
</div>



Below one is the employee.component.ts


import Component, OnInit from '@angular/core';
import NgForm from '@angular/forms';
import EmployeeService from '../shared/employee.service';
import Employee from '../shared/employee.model';


@Component(
selector: 'app-employee',
templateUrl: './employee.component.html',
styleUrls: ['./employee.component.css'],
providers: [EmployeeService]
)
export class EmployeeComponent implements OnInit

constructor(public employeeService: EmployeeService)

ngOnInit()
this.resetForm();


resetForm(form?: NgForm)
if (form)
form.reset();
this.employeeService.selectedEmployee =
name: '',
position: '',
office: '',
salary: null
;






Chrome thorough "ERROR TypeError: Cannot read property 'name' of undefined" error while loading the page initially. I tried many other resources dont know how to solve this.





change if (form) to if (true) and see if the error gone
– Ayoub k
Sep 4 at 6:05


if (form)


if (true)




5 Answers
5



add Safe Navigation Operator ( ?. ) becouse of employeeService?.selectedEmployee undefined


Safe Navigation Operator ( ?. )


employeeService?.selectedEmployee



Stackblitz demo



component.html


<input type="text" name="name" #name="ngModel" [(ngModel)]="employeeService?.selectedEmployee.name" placeholder="Enter full name" required>



component.ts


import Component,ViewChild from '@angular/core';
import EmployeeService from './employee.service';
import NgForm from '@angular/forms';

@ViewChild('myForm') myForm:any;
constructor(public employeeService: EmployeeService)

ngOnInit()
this.resetForm(this.myForm);


resetForm(form?: NgForm)
if (form)
form.reset();
this.employeeService.selectedEmployee =
name: '',
position: '',
office: '',
salary: null
;






I tried this, but doesn't allow it and throws error => Parser Error: The '?.' operator cannot be used in the assignment at column 40 in [employeeService?.selectedEmployee?._id=$event] in @10:61
– Srinath Srs
Sep 4 at 6:26





Let me update answer
– Krishna Rathore
Sep 4 at 6:39





I have update answer
– Krishna Rathore
Sep 4 at 7:03





StackBlitz demo also not working... i have attached the ss below here in the comment section.
– Srinath Srs
Sep 4 at 7:24



Cannot read property 'name' of undefined”



Means that some reference to name is wrong. There is only one in your code : employeeService?.selectedEmployee.name, therefore employeeService?.selectedEmployee is undefined.


employeeService?.selectedEmployee.name


employeeService?.selectedEmployee


undefined



Fix



Make sure selectedEmployee is not undefined, or use safe navigation employeeService?.selectedEmployee?.name


selectedEmployee


employeeService?.selectedEmployee?.name





I tried this, but it throws error initially => Parser Error: The '?.' operator cannot be used in the assignment at column 40 in [employeeService?.selectedEmployee?._id=$event] in @10:61
– Srinath Srs
Sep 4 at 6:37



Try this.


<input type="text" name="name" #name="ngModel" [(ngModel)]="employeeService?.selectedEmployee?.name" placeholder="Enter full name" required>



Add another ? after the selectedEmplyee. It will solve the problem.


?



The reason for the error is when the application starts the selectedEmplyee in employeeService is undefined. So make sure that variable is not undefined.


selectedEmplyee


employeeService





I tried this, but it throws error initially => Parser Error: The '?.' operator cannot be used in the assignment at column 40 in [employeeService?.selectedEmployee?._id=$event] in @10:61
– Srinath Srs
Sep 4 at 6:31



you need to instantiate the employeeService.selectedEmployee object so it won't be undefined instantiate the object in the constructor or ngOnInit() and you can not use Safe Navigation Operator ( ?. ) in ngModel



eg:


ngOnInit()
this.employeeService.selectedEmployee=new Employee ();





it worked... thank you... :)
– Srinath Srs
Sep 4 at 7:14





u welcome. glad i can help
– h_w
Sep 4 at 7:16



@Krishna Rathore
Screenshot of StackBlitz



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ