0

I'm looking to make a post request that sends some information such as the first name, last name, email, mobile number, and salary in Angular. However since the ".subscribe" method has been deprecated i'm unable to use it. How can I change this so that it works?

  postEmployeeDetails(){
    this.employeeModelObj.firstName = this.formValue.value.firstName;
    this.employeeModelObj.lastName = this.formValue.value.lastName;
    this.employeeModelObj.email = this.formValue.value.email;
    this.employeeModelObj.mobile = this.formValue.value.mobile;
    this.employeeModelObj.country = this.formValue.value.country;

    this.api.postUser(this.employeeModelObj)
      .subscribe(res=>{
        console.log(res);
        alert('User added successfully')
      },
      err=>{
        alert("something went wrong")
      })

This is my PostUser post request:

export class ApiService {

  constructor(private http : HttpClient) { }

  postUser(data : any) {
    return this.http.post<any>("http://localhost:3000/posts", data)
    .pipe(map((res:any)=>{
      return res;
    }))
  }
R. Richards
  • 24,603
  • 10
  • 64
  • 64
Sizzler
  • 73
  • 7

1 Answers1

2

you just give it an object instead:

 this.api.postUser(this.employeeModelObj)
      .subscribe({
          next:(res)=>{
              console.log(res);
          alert('User added successfully')
         },
         error: (err)=>{
             alert("something went wrong")
         }
     })

the subscription object contains next: ([obsevable type])=>{}, error: ([any])=>{}, complete: ()=>{} all optional