-2

Hi I am working on a angular web api project and I am getting this error on subscribe keyword, also imported observable but error is same. So if anyone has encounter this before kindly help.

employee.component.ts

import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import { EmployeeService } from 'src/app/shared/employee.service';
import 'rxjs/add/operator/maps';
import { Observable } from 'rxjs';

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

  ngOnInit(): void {
    this.resetForm();
  }
  resetForm(form?: NgForm) {
    if (form != null)
      form.resetForm();
    this.service.formData = {
      EmployeeID: null,
      FullName: '',
      Position: '',
      EMPCode: '',
      Mobile: ''
}
  }
  onSubmit(form: NgForm) {
    if (form.value.EmployeeID == null)
      this.insertRecord(form);
  }
      insertRecord(form: NgForm) {
        this.service.postEmployee(form.value).subscribe(res  => {           
     
         this.resetForm(form);
       
        });
  }
}
Joy Josh
  • 1
  • 2
  • 2
  • You need to share at least the code for the `postEmployee` function, otherwise we're not able to help you – Batajus Jan 12 '22 at 08:40
  • did you use ```return``` on your ```postEmployee()``` service? – Kibé M.C Jan 12 '22 at 09:27
  • yes after using the return method, I am getting error at this.service.postEmployee line Code : insertRecord(form: NgForm) { return{ this.service.postEmployee(form.value).subscribe((res: any[]): void=> { this.toastr.success('Inserted successfully', 'Employee Details'); this.resetForm(form); this.service.refreshList(); }), } } @KibéM.C – Joy Josh Jan 12 '22 at 11:50
  • Pretty sure you did not return anything from the employee.service.ts file in whatever you are using from the class EmployeeService. Do that and the problem will be fixed – ananvodo Dec 03 '22 at 21:09

1 Answers1

0

Try this...

import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';

insertRecord(form: NgForm):Observable<any> {
 return this.service.postEmployee(form.value).pipe(map(((res: any[]): void=> { 

this.toastr.success('Inserted successfully', 'Employee Details'); 

this.resetForm(form); this.service.refreshList(); }), } } 
Kibé M.C
  • 243
  • 3
  • 8