1

I have this message Error in angular:

src/app/components/employee/employee.component.html:67:92 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

67 <button class="btn btn-danger btn-sm mr-2 (click)="deleteEmployee(employee._id)">
                                                                                              

  src/app/components/employee/employee.component.ts:8:16
    8   templateUrl: './employee.component.html',
Error occurs in the template of component EmployeeComponent

enter image description here

Model : Employee

enter image description here

Service :

enter image description here

JavaScript Function:

enter image description here

Please a need know why this happened

Smit Patel
  • 1,682
  • 18
  • 23

1 Answers1

0

I think the value being passed in for _id is "uninitialized" and therefore its value is undefined. I can't tell without seeing more of your code. One way to fix this would be when you instantiate the employee, make sure _id is actually being set to a value, otherwise set it to '' (blank string) and check that it has a value or not in your delete().

Another option would be to edit your delete function

deleteEmployee(_id: string | undefined){
     if(_id) { //Checks if the passed variable has a value
          ...
     } 
     else {
          // Error
     }
}
Benito
  • 52
  • 8
  • 1
    I managed to solve the error declaring it in my service object as empty, but your answer also works, thank you very much! – Jesus Andres Mar 10 '21 at 16:53