can’t update and delete employee because id field is undefined

Created application in spring boot as backend and angular as frontend , data is properly getting from API checked in Postman API plateform(perform all CRUD properly on POSTMAN Plateform) ,In Angular data fetched except id, employee is created properly through angular,but can’t perform operation of Update and delete , as it gives console error “Id is UNDEFINED”
enter image description here
enter image description here

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Employee } from '../model/employee';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
@Injectable({
  providedIn: 'root'
})
export class EmployeeService {
  private baseUrl = "http://localhost:8080/api/v1/employees";
  employees: Employee[] | undefined;
  constructor(private http: HttpClient) {}

  addEmployee(emp: Employee): Observable<Employee> {
    return this.http.post<Employee>(this.baseUrl, emp)
      .pipe(
        catchError(this.handleError)
      );
  }

  getAllEmployees(): Observable<Employee[]> {
    return this.http.get<Employee[]>(this.baseUrl)
      .pipe(
        catchError(this.handleError)
      );
      console.log(this.employees);
  }

  updateEmployee(emp: Employee): Observable<Employee> {
    const url = `${this.baseUrl}/${emp.id}`;
    return this.http.put<Employee>(url, emp)
      .pipe(
        catchError(this.handleError)
      );
  }

  deleteEmployee(emp: Employee): Observable<Employee> {
    const url = `${this.baseUrl}/${emp.id}`;
    return this.http.delete<Employee>(url)
      .pipe(
        catchError(this.handleError)
      );
  }

  private handleError(error: any) {
    console.error('API Error: ', error);
    return throwError(error);
  }
}

import { Component, OnInit } from '@angular/core';
import { Employee } from '../../model/employee';
import { FormBuilder, FormGroup } from '@angular/forms';
import { EmployeeService } from '../../service/employee.service';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
  empDetail !: FormGroup;
  empObj : Employee = new Employee();
  empList : Employee[] = [];

  constructor(private formBuilder : FormBuilder, private empService : EmployeeService) { }

  ngOnInit(): void {

    this.getAllEmployee();

    this.empDetail = this.formBuilder.group({
      id : [''],
      firstName : [''],
      lastName : [''],
      emailId : ['']
    });    
    this.getAllEmployee();
  }

  addEmployee() {
    console.log(this.empDetail);
    this.empObj.id = this.empDetail.value.id;
    this.empObj.firstName = this.empDetail.value.firstName;
    this.empObj.lastName = this.empDetail.value.lastName;
    this.empObj.emailId = this.empDetail.value.emailId;

    this.empService.addEmployee(this.empObj).subscribe({
     next: (res)=>{
        console.log(res);
        this.getAllEmployee();
    },error: err=>{
        console.log(err);
    }});

  }

  getAllEmployee() {
    this.empService.getAllEmployees().subscribe({
      next:(res)=>{
        this.empList = res;
    },error:err=>{
      console.log("error while fetching data.")
    }});
  }

  editEmployee(employee: Employee) {
    if (!employee|| !employee.id) {
      console.error('Employee data or ID is undefined');
      return;
    }
  
    this.empDetail.setValue({
      id: employee.id || '',
      firstName: employee.firstName || '',
      lastName: employee.lastName || '',
      emailId: employee.emailId || ''
    });
  }
  updateEmployee() {
    if (!this.empObj || !this.empObj.id) {
      console.error('Employee data or ID is undefined');
      return;
    }
    this.empObj.id = this.empDetail.value.id;
    this.empObj.firstName = this.empDetail.value.firstName;
    this.empObj.lastName = this.empDetail.value.lastName;
    this.empObj.emailId = this.empDetail.value.emailId;

    this.empService.updateEmployee(this.empObj).subscribe({
      next:(res)=>{
      console.log(res);
      this.getAllEmployee();
    },error: err=>{
      console.log(err);
    }})

  }

  deleteEmployee(emp : Employee) {
    if (!emp || !emp.id) {
      console.error('Employee data or ID is undefined');
      return;
    }
  
    this.empService.deleteEmployee(emp).subscribe({
      next: (res) => {
        console.log(res);
        alert('Employee deleted successfully');
        this.getAllEmployee();
      },
      error: (err) => {
        console.log(err);
      }
    });
  }

}

enter image description here

enter image description here

I am trying to add more validation to find exact problem , but conclusion I am getting is there is something blocking ID field in angular side so can’t perform operation, I just want to help to resolve this problem

New contributor

swati kadu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

UPDATE:

You need to rename all id properties into empid, same things needs to be done for HTML.

You are having empid returned from java layer, but you are trying to access id field which does not exist.


The get route of java code, is not returning the employee id field, fix that and the update and delete will start working.

In the below screenshot you can see there is no ID field values.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật