Problem
When click button edit on employee list it not open page or form of edit and show error
ERROR TypeError: Cannot read property 'toString' of undefined at EmployeeListComponent.push../src/app/employeelist.component.ts.EmployeeListComponent.editEmp (employeelist.component.ts:35)
this is error on line localStorage.setItem('editEmpId', employee.EmployeeId.toString());
on employeelist.component.ts i have function editemp as following
editEmp(employee: Employee): void { localStorage.removeItem('editEmpId'); console.log(employee.EmployeeId) ; localStorage.setItem('editEmpId', employee.EmployeeId.toString()); this.router.navigate(['employees']); }
on employeelist.component.html
<tr *ngFor = "let emp of employees"><td> {{emp.employeeId}}</td><td> {{emp.branchCode}}</td><td> {{emp.employeeName}}</td><td><button (click)="editEmp(emp.employeeId)" class="btn btn-sm btn-outline-danger">Edit</button></td>
and this form employees.component.ts
import { Component } from '@angular/core' import { ApiService } from './api.service' import { NgForm } from '@angular/forms'; import { ToastrService } from 'ngx-toastr'; import { FormBuilder, FormGroup, Validators } from "@angular/forms"; import { Router } from "@angular/router"; @Component({ selector :'employees', templateUrl : './employees.component.html' }) export class EmployeesComponent{ employees = {} empformlabel: string = 'Add Employee'; empformbtn: string = 'Save'; constructor(private formBuilder: FormBuilder ,private router: Router , private api:ApiService,private toastr: ToastrService){} addForm: FormGroup; btnvisibility: boolean = true; ngOnInit() { this.addForm = this.formBuilder.group({ EmployeeId: [], EmployeeName: ['', Validators.required], JoinDate: ['', [Validators.required, Validators.maxLength(20)]], BirthDate: ['', [Validators.required, Validators.maxLength(20)]], EmployeeAge: ['', [Validators.required, Validators.maxLength(3)]], BranchCode: ['', [Validators.required, Validators.maxLength(2)]], Active: ['', Validators.required] }); //this.resetForm(); let empid = localStorage.getItem('editEmpId'); if (+empid > 0) { this.api.getEmployeeById( +empid).subscribe(data => { this.addForm.patchValue(data); }) } this.btnvisibility = false; this.empformlabel = 'Edit Employee'; this.empformbtn = 'Update'; } }
How to solve this error please ?